Jump to content

nested loop - need help


knightsjoker

Recommended Posts

Hi guys,

 

Was wondering if anyone can help me with this. Here's what I'm trying to do:

 

I've retrieved values from table a from mysql and post them on a form.

 

echo "<tr><td>" .$row['id']. "</td><td>" .$row['name']. "<input type='hidden' name=name[] value=" .$row['name']. "></td><td><select name='item[]'><option value ='' selected='selected'>Please Select</option><option value ='1'>1</option><option value ='2'>2</option><option value ='3'>3</option></td></tr>";

 

and now i'm trying to display these information one more time on a different page. So I did the usual retrieval

 

$name = $_POST['name']; 
$item = $_POST['item']; 

 

Then I'm using foreach loop to retrieve the item values.

 

foreach ($item as $getitem => $value)     
{ 
echo "<tr><td>"; 
echo $value; 
echo "</td></tr>"; 
} 

 

Here is where I'm stumped. What's the best way to associate the name to the item.

 

while loop?

 

Thanks for the help.

Link to comment
Share on other sites

They'll be in a numerically-indexed array, so you can use a for() loop to go through them...

 

I was thinking the same thing - but if the form is manipulated in any way to submit less names than items or vice versa then the numeric indices won't match up.  Not sure how serious this is in the context of the application though...

 

Something for the OP to think about anyway.

Link to comment
Share on other sites

I got it.

 

I had to rewrite the table view using for loop and do the counting since the form is dynamic.

 

and so the table view will be like:

 

echo "<tr><td>" .$row['id']. "</td><td>" .$row['name']. "<input type='hidden' name='varname[" .$j. "][" .$i. "]'  value='" .$row['name']. "'></td><td><select name='quantity[" .$j. "][" .$i. "]'><option value ='0' selected='selected'>0</option><option value ='1'>1</option><option value ='2'>2</option><option value ='3'>3</option></td></tr>";
$i++;

 

then you do the usual array retrieval

and to spew it back out...



for($j=1; $j<=count($itemname); $j++)
{
	for ($i=0; $i<count($itemname[$j]); $i++) 
	{
		$getvalues1 = $itemname[$j][$i];
		$getvalues2 = $itemquantity[$j][$i];
    		echo "<li>";
    		echo "<input type='text' name='itemname' value='" .$getvalues1. "' readonly='readonly'>";
    		echo "</li>";
    		echo "<li>";
    		echo "<input type='text' name='itemquantity' value='" .$getvalues2. "' readonly='readonly'>";
	}	
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.