Page 1 of 1

foreach loop problem

Posted: Thu Feb 22, 2007 1:01 pm
by vzwhaley
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am creating a restaurant directory and have two tables, one named Restaurants and another named RestaurantsCuisine. I have created an array of Cuisine ID values that I store in a field called Cuisines in the Restaurants table. I then explode the Cuisine ID values and am trying to check multiple checkboxes in an edit script for the restaurant. The Cuisine IDs in the array from the Restaurants table should match with ID fields from the RestaurantsCuisine table.

I cannot get the foreach loop to work correctly. It will check one of the checkboxes, but not all of the checkboxes that need to be checked. I have tested the foreach  statement to ensure that it is pulling in each Cuisine ID and it is successful, but I just cannot get the loop to work correctly. Any help would be appreciated.

Code: Select all

<?
$sql = "SELECT * FROM Restaurants WHERE ID = '4' ";
	$Recordset = mysql_query($sql);
	$RS = mysql_fetch_assoc($Recordset);
	
	$sql1 = "SELECT * FROM RestaurantsCuisine ORDER BY CuisineType ASC";
	$Recordset1 = mysql_query($sql1);


$Cuisine = $RS['Cuisine'];
$CuisineListS = explode("|", trim($Cuisine));

while ($row_Recordset1 = mysql_fetch_array($Recordset1)) {
?>

<input name="Cuisine[]" type="checkbox" value="<?php echo $row_Recordset1['ID']; ?>"<?php 
foreach ($CuisineListS as $CuisineList2) {
	if($CuisineList2 == $row_Recordset1['ID']) {
       echo " CHECKED";
	   }
	  }
?>

><?php echo $row_Recordset1['CuisineType']; ?><br>
<? } ?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Feb 22, 2007 2:27 pm
by jyhm
It doesn't look to me that the line:

Code: Select all

<input name="Cuisine[]" type="checkbox" value=
is in a loop and that it is all by itself meaning that it only gets written once. I thinl your php start and end tags may be causing some issues, why are you using so many?