Jump to content

PHP variable or looping problems


DerrickE

Recommended Posts

The problem is with the variable variable $secid[$j] or the loop not the database part.

 

Basically my database is setup like this:

 

sections -> id,sid,name

menu -> item,sid

 

the menu sid corresponds with the section id

 

The code below is supposed to pull the section id from the sections table and then for every section, it is supposed to pull the menu item that belongs in that section.

 

Section1 has 3 items

item1 from section1

item2 from section1

item3 from section1

 

Section2 has 1 item

item1 from section2

item2 from section2

 

Section3 has 4 items

item1 from section3

item2 from section3

item3 from section3

item4 from section3

 

 

The first loop works fine, it picks up the correct section ids.

The second loop to pick up the actual items in the section fails. It acts like it is looping through(ie. every section gets the # of items it actually has) But the data is from the 1st section only. ie, the 2nd and 3rd and 4th sections all contain the data from the 1st section.

 

 

It actually looks like this: (please note the section# from which it is pulling the item....different from above)

 

Section1 has 3 items

item1 from section1

item2 from section1

item3 from section1

 

Section2 has 1 item

item1 from section1

item2 from section1

 

Section3 has 4 items

item1 from section1

item2 from section1

item3 from section1

 

 

 

 

 

$sql = "SELECT * FROM sections where rid='$rid' ORDER BY sid";
$content .= $sql."<br>";
$modextra = "<hr><h1>Non-Sectioned</h1>";
$result = mysql_query($sql) or die(mysql_error());
$nrows = mysql_num_rows($result);
while($row = mysql_fetch_array($result)) {
    $secid[] = $row['id'];
    $sname[] = $row['name'];
}
for($j = 0; $j < $nrows; $j++) {
$content .= "<hr><h1>$sname[$j]</h1>";
$content .= "<TABLE BORDER=1 cellpadding=5 cellspacing=0 align=center width=100%>\n";
$sql = "SELECT * FROM menu where rid='$rid' AND sid='${$secid[$j]}' ORDER BY id";
$content .= $sql."<br>";
$result = mysql_query($sql) or die(mysql_error());
$rows = mysql_num_rows($result);
while($row = mysql_fetch_array($result)) {
    $id[] = $row['id'];
    $iname[] = $row['iname'];
    $idesc[] = $row['idesc'];
    $iprice[] = $row['iprice'];
    $iextra1[] = $row['extra1'];
    $iextra2[] = $row['extra2'];
    $isid[] = $row['sid'];

}
for($i = 0; $i < $rows; $i++) {
    $content .= " <TR>\n";
    $a = $i;
            $content .= "<TD valign=top width=200><font style=\"font-size:8pt;\"><b>ID:</b> ". $id[$a] ."<br><b><a name=\"". $id[$a] ."\">Name:</a></b> ". $iname[$a]."<br><b>Price: </b>".$iprice[$a]."</font></td><td><font style=\"font-size:8pt;\"><b>Extra1: </b>".$iextra1[$a]."<br><b>Extra2: </b>".$iextra2[$a]."</font></td><td><b>Description:</b><br> ". $idesc[$a] ."</td><td><input type=button value='Delete Item' onclick=\"document.location='menuitemdelete.php?i=confirm&rid=".$rid."&id=$id[$a]'\"><br><input type=button value='Modify Item' onclick=\"document.location='menu_itemmod.php?id=$id[$a]'\"></td>";
    $content .= " </TR>\n";
}
$content .= "</TABLE>\n";

Link to comment
https://forums.phpfreaks.com/topic/73238-php-variable-or-looping-problems/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.