Hi guys,
I am having trouble with a PHP loop - I simply cannot think about how I can get my database query to display the way I want it to. I was wondering if someone could please help me.
Essentially I have a table in my MySQL database that looks like this:
Library Shelf Book
Green Library Shelf A Book 1
Green Library Shelf A Book 2
Green Library Shelf B Book 3
Green Library Shelf B Book 4
Green Library Shelf B Book 5
Green Library Shelf B Book 6
Blue Library Shelf A Book 7
Blue Library Shelf A Book 8
Blue Library Shelf A Book 9
Blue Library Shelf A Book 10
Blue Library Shelf B Book 11
Yellow Library Shelf A Book 12
Yellow Library Shelf A Book 13
Yellow Library Shelf A Book 14
Now, I want to be able to loop through the results so the results look like this:
Green Library
Shelf A: Book 1, Book 2
Shelf B: Book 3, Book 4, Book 5, Book 6
Blue Library
Shelf A: Book 7, Book 8, Book 9, Book 10
Shelf B: Book 11
Yellow Library
Shelf A: Book 12, Book 13, Book 14
I just want the above to be simple text, not in any table structure or anything. The only rules are:
- New line of text per Library
- New line of text per Shelf
- Each book separated by a comma.
Can someone please shed some light on what a loop for this might looks like? So far I have the standard loop query:
$result = mysql_query($q) or die(mysql_error());
$num_rows = mysql_numrows($result);
for($i=0; $i<$num_rows; $i++){
//INSERT CODE HERE
}
I have tried several combinations of loops inside this code to try and get what I want but I have had a bit of a meltdown and I can't get what I want. Any help would be appreciated.