0

I'm working on a script that writes out a form based on an uploaded CSV file. The CSV file contains sections that are separated by a line containing only ',,,,'. Each block contains information about a server that is going to be kickstarted. It looks like this:

host,tctivt2r6ra03,,,
int,eth0,10.153.196.248,255.255.255.0,10.153.196.1
int,eth1,10.153.157.113,255.255.255.128,10.153.157.1
int,eth2,10.153.157.241,255.255.255.128,10.153.157.129
int,eth3,00:50:56:ac:69:cb,,
part,/home,10,,
part,swap,10,,
part,/opt,60,,
part,/data,30,,
,,,,

This input is written to a div which contains a table. Each row of input being a unique row in the table. Some values end up in text boxes.

The script generates output that looks like this.

Each box is generated dynamically and consists of the host ID starting at 0 and the box number also starting at 0. For example:

<tr><th>eth0: </th><td><input type="text" value="10.153.196.248" name="host0Box0" /></td><td><input type="text" value="255.255.255.0" name="host0Box1" /></td><td><input type="text" value="10.153.196.1" name="host0Box2" /></td></tr><tr>

The question I have is how to process a form that has a variable number of boxes. Specifically, when processing the 'part' lines of the input there can be six boxes or more.

Example with four partitions:

<tr><th>Partition name:</th><td> /</td><td>10</td></tr>
<tr><th>Partition name:</th><td> /var</td><td><input type="text" value="10" name="host0Box9" /></td></tr>
<tr><th>Partition name:</th><td> /home</td><td><input type="text" value="10" name="host0Box10" /></td></tr>
<tr><th>Partition name:</th><td> swap</td><td><input type="text" value="10" name="host0Box11" /></td></tr>
<tr><th>Partition name:</th><td> /opt</td><td><input type="text" value="60" name="host0Box12" /></td></tr>
<tr><th>Partition name:</th><td><input type="text" value="/data" name="host0Box13" />/td><td><input type="text" value="30" name="host0Box14" /></td></tr>
<tr><th>Number of Disks: </th><td><input type="text" value="1" name="host0Disks" /></td></tr></table></div><span><br /><br /></span>

Example with six partitions:

<tr><th>Partition name:</th><td> /</td><td>10</td></tr>
<tr><th>Partition name:</th><td> /var</td><td><input type="text" value="10" name="host1Box9" /></td></tr>
<tr><th>Partition name:</th><td> /home</td><td><input type="text" value="10" name="host1Box10" /></td></tr>
<tr><th>Partition name:</th><td> swap</td><td><input type="text" value="10" name="host1Box11" /></td></tr>
<tr><th>Partition name:</th><td> /opt</td><td><input type="text" value="60" name="host1Box12" /></td></tr>
<tr><th>Partition name:</th><td><input type="text" value="/data" name="host1Box13" /></td><td><input type="text" value="30" name="host1Box14" /></td></tr>
<tr><th>Partition name:</th><td><input type="text" value="/test1" name="host1Box15" /></td><td><input type="text" value="30" name="host1Box16" /></td></tr>
<tr><th>Partition name:</th><td><input type="text" value="/test2" name="host1Box17" /></td><td><input type="text" value="20" name="host1Box18" /></td></tr>
<tr><th>Number of Disks: </th><td><input type="text" value="1" name="host0Disks" /></td></tr></table></div><span><br /><br /></span>

This is a work in progress that I know I'm going to have to change in order to properly process each host section. In the meantime, I'm trying to sort out this one issue.

My question right now is, when I I eventually do get to the point of processing the form, what approach should I use to account for dynamically allocated item names when the $_POST array is populated? Do I pull the array values out and place them in a different array? Can I iterate over the $_POST array like a normal array? Is there another option that I'm not aware of?

theillien
  • 1,189
  • 3
  • 19
  • 33

2 Answers2

2

Looks like you're after this?

So instead of name="host0Box0" use name="host[0][Box][] assuming you can change the script output.

Using [] will automatically fill out an array so for example

<input type="text" name="foo[]" value="Hello" />
<input type="text" name="foo[]" value="World" />

will give you the array

Array(
  [foo] => Array(
    [0] => Hello
    [1] => World
  )
)

I may have misunderstood the question.

PieSub
  • 318
  • 1
  • 8
  • This looks promising. I'll play around with it. Thanks. – theillien Nov 14 '12 at 16:02
  • The code I have calls various functions to enter the different rows for the table. One for the hostname, one for the IP addressing information and another for partition information. An example of such a function is `function printHost($hostName, $hostID, $boxNum) { print 'Hostname: – theillien Nov 15 '12 at 21:33
  • I went with `"host' . $hostID . 'Box[]"` instead. That seems more proper than `"host[' . $hostID . ']Box[]"`. – theillien Nov 15 '12 at 22:22
  • I would suggest going with `name="host[' . $hostID . ']Box[]"` because you'll be able to iterate it easily with `foreach($_POST['host'] as $hostId=>$box) { ... }` where $box would be an array of all the inputs. – PieSub Nov 15 '12 at 23:08
  • *edit* You don't actually need Box: `name="host[' . $hostID . '][]"` would work fine. – PieSub Nov 15 '12 at 23:14
  • As an example, this is what I'm looking for, yes? `name="host[1][]"` – theillien Nov 15 '12 at 23:38
  • That's it. To get a nice look at how the data is being passed use something like `echo "
    ";print_r($_POST);` Should help you understand what's going on.
    – PieSub Nov 15 '12 at 23:53
  • Thanks. I appreciate the assistance. – theillien Nov 16 '12 at 00:05
0

You can iterate over the superglobal $_POST array just as you can any array. Try var_dump($_POST) to see whats available.

Since the posted names are unknown, you may need to parse the raw input stream if these names contain unusual characters. The Accepted answer to Question 813487 may be helpful.

Community
  • 1
  • 1
mstrthealias
  • 2,781
  • 2
  • 22
  • 18