I have created a multipage form which collects data input by the user before and is then sent by email.

I have created a table that the user has to fill in an ammount for each item. They user doesn't have to fill everything in only the items that apply.

This is a sample of the table.
It haa a text input which holds the amount and a hidden value that holds the description so they can he eched out next to each other later for example "Item 1: 100"

<table>
    <tr>
        <th>Description</td>
        <th>Amount</td>
        <th>Description</td>
        <th>Amount</td>
    </tr>
    <tr>
        <td>Item 1</td>
        <td><input type="text" name="amount[]">
        <input type="hidden" name="description[]" value="Item 1">
        </td>
        <td>Item 2</td>
        <td><input type="text" name="amount[]">
        <input type="hidden" name="description[]" value="Item 2">
        </td>
    </tr>
    <tr>
        <td>Item 3</td>
        <td><input type="text" name="amount[]">
        <input type="hidden" name="description[]" value="Item 3">
        </td>
        <td>Item 4</td>
          <td><input type="text" name="amount[]">
        <input type="hidden" name="description[]" value="Item 4">
        </td>
    </tr>

I want to loop out this data using a while loop however only one value is being echoed not both.

while(list(, $description) = each($_POST['description']) && list(, $amount) = each($_POST['amount'])){
echo $description ." " . amount . " ";
}
Member Avatar for diafol

I don't get the need for the hidden input.

$num_display = 100;
$output = '';

for($x=0;$x<$num_display;$x+2)
{
    $output .=  '<tr><td>Item '.($x+1).'</td><td><input type="text" name="amount[]" placeholder="Item '.($x+1).'"></td><td>Item '.($x+2).'</td><td><input type="text" name="amount[]" placeholder="Item '.($x+2).'"></td></tr>';
}

echo $output;

Then in your data handling script...

$out ='';
foreach($_POST['amount'] as $index=>$value)
{
    if(trim($value) != ''){
        $out .= 'Item ' . ($index + 1) . ': ' . $value . '<br />';
    }
}
echo $out;

I'm not sure about the counter ($x+2) - it may fail at the end if you choose odd number of $x values. Also, tables are fiddly things to get right. You may find inline <ul> lists better or even form css3 *-column-count from a straight list.

I need the hidden value as the Description isn't actually item 1, item 2, item 3 I just used them as an example. I should have made that clear. I would have used a foreach loop if it was just using one post value but I couldn't get it to work with two post values.

Member Avatar for diafol

Your way looks as though you're hardcoding all these descriptions into the table. WHy not store them in an array?

$desc = array('Not really Item 1', 'Not really called item 2 either', 'Dog knows what this is',...);
$num_display = 100;
$output = '';
for($x=0;$x<$num_display;$x+2)
{
    $output .=  '<tr><td>Item '.($x+1).'</td><td><input type="text" name="amount[]" placeholder="'. $desc[($x+1)].'"></td><td>Item '.($x+2).'</td><td><input type="text" name="amount[]" placeholder="'. $desc[($x+2)].'"></td></tr>';
}
echo $output;

Then...

$out ='';
foreach($_POST['amount'] as $index=>$value)
{
    if(trim($value) != ''){
        $out .= $desc[$index] . ': ' . $value . '<br />';
    }
}
echo $out;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.