I have 3 MySQL tables:
shelf
---------------
id
project_id
label
books
---------------
id
project_id
label
booksOnShelves
---------------
id
shelf_id
book_id
project_id
user_id
I need to display what books are on what shelf in a table format, something like:
History Fiction
------- --------
book 1 book 4
book 2 book 5
book 3
So far I’ve got only this:
SELECT
shelf.label,
books.label
FROM booksOnShelves
LEFT JOIN shelf ON (shelf.id = booksOnShelves.shelf_id)
LEFT JOIN books ON (books.id = booksOnShelves.book_id)
WHERE booksOnShelves.project_id = ".$_POST['pid']."
AND booksOnShelves.user_id = ".$_POST['uid']."
Couple things… For some odd reason shelf label is not showing up, but if I remove all other parts in the query, like book labels, it works. Also, I can only display in one long column (via loop). How would I break it into separate coulmns, or if in one column, how do I show shelf label only once per group of books?
1st: escaping the input vars
2nd: remove LEFT in join, if you start from cross table leftjoin is nonsense (IMHO)
3rd: use GROUP_CONCAT to return a string of set of titles, comma separated, 1 row per shelf
Of course your PHP has to parse/manage titlelist to fill the page
ADD
You can change the separator as your needs (eg: |) then
I hypothesized $results is an associative array with all rows of your query.