Dave.H
12-17-2008, 04:46 PM
Hello all,
Ok so I have a script that pulls category names from a database. I then fetch it from the db and print the category using a while loop inside that same while loop I print out each of the corresponding items that belong in that category within another while loop. So the category gets printed then the items in it.
My problem is I want the categories to randomly change order on page refresh which means the items also have to change order. I was toying with storing it all in arrays and shuffle() them but then how do you know which items go with which category after because the shuffle() assigns new keys...
Anyways heres my code the items always have to be under the category they belong and the categories should appear randomly on refresh:
$query = "SELECT name,id
FROM category
WHERE id='1'";
$mysql_stuff = mysql_query($query, $mysql_link);
while($row = mysql_fetch_row($mysql_stuff)) {
// populate the variables from the parent table for a single record
$category = $row[0];
$id = $row[1];
print("<h2>$category</h2>");
$query2 = "SELECT name
FROM restaurants
WHERE category_id='$id'";
$mysql_stuff2 = mysql_query($query2, $mysql_link);
while($row2 = mysql_fetch_row($mysql_stuff2)) {
// populate the variables from the parent table for a single record
$restaurants = $row2[0];
print("<p>$restaurants</p>");
}
}
Here is the sample of it working:
http://www.prevumenu.com/stittsville/
I need Italian to switch places with casual dining on refresh.
Ok so I have a script that pulls category names from a database. I then fetch it from the db and print the category using a while loop inside that same while loop I print out each of the corresponding items that belong in that category within another while loop. So the category gets printed then the items in it.
My problem is I want the categories to randomly change order on page refresh which means the items also have to change order. I was toying with storing it all in arrays and shuffle() them but then how do you know which items go with which category after because the shuffle() assigns new keys...
Anyways heres my code the items always have to be under the category they belong and the categories should appear randomly on refresh:
$query = "SELECT name,id
FROM category
WHERE id='1'";
$mysql_stuff = mysql_query($query, $mysql_link);
while($row = mysql_fetch_row($mysql_stuff)) {
// populate the variables from the parent table for a single record
$category = $row[0];
$id = $row[1];
print("<h2>$category</h2>");
$query2 = "SELECT name
FROM restaurants
WHERE category_id='$id'";
$mysql_stuff2 = mysql_query($query2, $mysql_link);
while($row2 = mysql_fetch_row($mysql_stuff2)) {
// populate the variables from the parent table for a single record
$restaurants = $row2[0];
print("<p>$restaurants</p>");
}
}
Here is the sample of it working:
http://www.prevumenu.com/stittsville/
I need Italian to switch places with casual dining on refresh.