the title is a little vague on what I want to do so I'll do the explaining here. I have two tables that I get information from, the Book table and Inventory table. I get the Book Title, Author Name and ISBN from Book table, and I concatenate the Quantity and Library Location from Inventory into one. What I want is for the information to look like
Title of Book
By Author1 Name and Author2 Name
ISBN: 740-fojsd99
(0) Copies available at Location2
(3) Copies available at Location5
when I get the echo the information in php it only gets the first row of the Inventory table and echo likes this
22 11 63
By Stephen King
ISBN: 9788401344106
(0) Copies at the location of Sylvania Public Library
It should look like
22 11 63
By Stephen King
ISBN: 9788401344106
(0) Copies at the location of Sylvania Public Library
(2) Copies at the location of Toledo Public Library
(1) Copies at the location of Carson Library
I tried exploding, foreach and using two separate queries to get what I wanted to show but I am a bit of a noob with PHP and still learning how it works. My php code is as follows
$sql = "SELECT Book.Book_Title,
Book.Author_Name,
Book.Book_ISBN
FROM Book
GROUP BY Book_Title";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($result))
{
//var_dump($row);
//had var_dump($result->num_rows); here
echo "<b><u>". $row["Book_Title"] ."</u></b><br>";
echo "By ". $row["Author_Name"]. "<br>";
echo "ISBN: ". $row["Book_ISBN"] . "<br><br>";
$ISBN = $row['Book_ISBN'];
$sql2 = "SELECT CONCAT('<DD>(',Inventory.Quantity, ') Copies at the location of ', Inventory.Library_Location,'</DD>') as nCopies
FROM Inventory
JOIN Book
ON Inventory.Book_Id=Book.Book_Id
WHERE $ISBN = Book.Book_ISBN";
$result2 = mysqli_query($conn,$sql2);
while ($row = mysqli_fetch_assoc($result2))
{
echo "" . $row["numOfCopies"] . "<br>";
var_dump($row);
}
var_dump($row);
}
Doing so in my db
SELECT Book.Book_Title,
Book.Author_Name,
Book.Book_ISBN
FROM Book
GROUP BY Book_Title
Gave me the output
+-----------------------------------------+-------------------------------------------+---------------+
| Book_Title | Author_Name | Book_ISBN |
+-----------------------------------------+-------------------------------------------+---------------+
| 22 11 63 | Stephen King | 9788401344106 |
| A Good Marriage | Stephen King | 9781401104428 |
| Bag of Bones | Stephen King | 9780671024239 |
| Carrie | Stephen King | 9780307743664 |
| Cell | Stephen King | 9781416424419 |
| Christine | Stephen King | 9780441160447 |
| Cujo | Stephen King | 9780441161342 |
| Dark Souls: Design Works | From Software | 9781926778891 |
| Desperation | Stephen King | 9781101137994 |
| Doctor Sleep | Stephen King | 9781441698844 |
| Dragon Age Inquisition: Official Guide | David Knight | 9780804162944 |
| Duma Key | Stephen King | 9788401338090 |
| Everythings Eventual | Stephen King | 9780743447344 |
| Hearts in Atlantis | Stephen King | 9780671024246 |
| Heir to the Jedi: Star Wars | Kevin Hearne | 9780344444848 |
| Insomnia | Stephen King | 9781101138007 |
| IT | Stephen King | 9780441169418 |
| Joyland | Stephen King, Hannes Riffel | 9783641147074 |
| Mile 81 | Stephen King | 9781441664604 |
| Mr. Mercedes | Stephen King | 9781476744474 |
| Pet Sematary | Stephen King | 9780743412278 |
| Prince Caspian | C.S. Lewis, Pauline Baynes | 9780064404003 |
| process control 221 Success Secrets | David Knight | 9781488844672 |
| Secret Window | Stephen King | 9780441213469 |
| Skeleton Crew | Stephen King | 9780441168610 |
| Star Wars DarkSaber | Kevin J. Anderson | 9780307796417 |
| Star Wars I, Jedi | Michael A. Stackpole | 9780443478737 |
| Star Wars Mad Libs | Roger Price, Leonard Stern | 9780843132717 |
| Star Wars Red Harvest | Joe Schreiber | 9780344418490 |
| Star Wars The Essential Atlas | Jason Fry, Daniel Wallace | 9780344477644 |
| Star Wars: Choices of One | Timothy Zahn | 9780344411263 |
| Star Wars: Dark Empire Trilogy | Tom Veitch, Jim Baikie, Cam Kennedy | 9781302466434 |
| Star Wars: Jedi Academy | Jeffery Brown | 9780444404178 |
| Star Wars: Rebel Force: Hostage | Alex Wheeler | 9781484720226 |
| Star Wars: Riptide | Paul S. Kemp | 9780344422467 |
| Star Wars: Rise and Fall of Darth Vader | Ryder Windham | 9781484717874 |
| Tarkin: Star Wars | James Luceno | 9780344411422 |
| The Dead Zone | Stephen King | 9780441144747 |
| The Horse and His Boy | C.S. Lewis, Pauline Baynes | 9780064471060 |
| The Last Battle | C.S. Lewis, Pauline Baynes, David Wiesner | 9780064404034 |
| The Legend of Zelda: Hyrule Historia | Patrick Thorpe | 9781616440417 |
| The Lion, the Witch and the Wardrobe | C.S. Lewis, Pauline Baynes | 9780064471046 |
| The Magicians Nephew | C.S. Lewis, Pauline Baynes | 9780064471107 |
| The Mist | Stephen King | 9780441223296 |
| The Running Man | Stephen King, Richard Bachman | 9780441197962 |
| The Shining | Stephen King | 9780344806789 |
| The Stand | Stephen King | 9780307743688 |
| The Tommyknockers | Stephen King | 9780441146600 |
| Thinner | Stephen King, Richard Bachman | 9780441161344 |
| Under the Dome | Stephen King | 9781476734474 |
+-----------------------------------------+-------------------------------------------+---------------+
and doing
SELECT CONCAT('<DD>(',Inventory.Quantity, ') Copies at the location of ', Inventory.Library_Location,'</DD>') as nCopies
FROM Inventory
JOIN Book
ON Inventory.Book_Id=Book.Book_Id
(had to remove GROUP BY Book_Title because it said Column 'Book_Id' in group statement is ambiguous) gave me the results
+----------------------------------------------------------------+
| nCopies |
+----------------------------------------------------------------+
| <DD>(0) Copies at the location of Sylvania Public Library</DD> |
| <DD>(1) Copies at the location of Carson Library</DD> |
| <DD>(2) Copies at the location of Commuter Public Library</DD> |
| <DD>(0) Copies at the location of Sylvania Public Library</DD> |
| <DD>(2) Copies at the location of Toledo Public Library</DD> |
| <DD>(1) Copies at the location of Carson Library</DD> |
| <DD>(1) Copies at the location of Carson Library</DD> |
| <DD>(2) Copies at the location of Commuter Public Library</DD> |
| <DD>(2) Copies at the location of Toledo Public Library</DD> |
| <DD>(2) Copies at the location of Sylvania Public Library</DD> |
+----------------------------------------------------------------+
Edit: Last Edit the while loops worked and results came back as follows
22 11 63
By Stephen King
ISBN: 9788401344106
(0) Copies at the location of Sylvania Public Library
(2) Copies at the location of Toledo Public Library
(1) Copies at the location of Carson Library
A Good Marriage
By Stephen King
ISBN: 9781401104428
(1) Copies at the location of Carson Library
(2) Copies at the location of Commuter Public Library
Bag of Bones
By Stephen King
ISBN: 9780671024239
(2) Copies at the location of Toledo Public Library
(2) Copies at the location of Sylvania Public Library
Cell
By Stephen King
ISBN: 9781416424419
(0) Copies at the location of Sylvania Public Library
(1) Copies at the location of Carson Library
(2) Copies at the location of Commuter Public Library
Thank you @nomistic for the help, appreciate it!