Web Hosting Talk







View Full Version : PHP: Nested foreach statements giving me problems


[gg]swift
05-02-2004, 02:43 AM
Ok, this is going to be hard to explain but I'll try,

I have a page where an admin can modify member ranks, It lists the members and then next to the name there is a drop down menu for each member with some ranks: Admin, Member, Moderator etc.

Now, I have a hidden field member_id[] and the drop menu has the name rank[]. When the form is submitted I have:


foreach ($_POST[member_id] as $member_id) {

foreach ($_POST[rank] as $rank) {

Then here is where I update the mysql database with the values

}


}


But I keep getting double results and it then it only changes the last members information. I kinda solved the problem with array_combine in PHP5 but if the two variable's values are the same it returns an error, so I am looking for an alternative. Thanks.

t3r0
05-02-2004, 11:51 AM
try something like this..

FORM:

<input type="hidden" name="member[id][]" value="xxxxxx">
<select name="member[rank][]">
<option value="admin">admin</option>
<option value="moder">moder</option>
<option value="...">...</option>
</select>


and PHP:

foreach ($_POST['member']['id'] as $key => $Value)
{
mysql_query("UPDATE table_name SET rank = $_POST['member']['rank'][$key] WHERE userID = $Value LIMIT 1;");

}



NOT tested, but hope this helps you out..

-- t3r0 :)

silhouette
05-02-2004, 12:25 PM
a fast get around....Can this work???


$i=0;
foreach ($_POST[member_id] as $member_id) {
//populate using $member_id and $_POST['rank'][$i]
$i++;
}