The following two-dimensional array stores information for books (1.ID, 2.Name, 3.Author ID, 4.Author Name, 5.Publisher ID, 6. Publisher Name, 7. Publisher Reputation):
$books = array(array('book_id'=>1, 'book_name'=>"BookA", 'author_id'=1,
'author_name'=>"AuthorA", 'publisher_id'=>1,
'publisher_name'=>"PublisherA", 'publisher_reputation'="good"),
array('book_id'=>1, 'book_name'=>"BookA", 'author_id'=1,
'author_name'=>"AuthorA", 'publisher_id'=>2,
'publisher_name'=>"PublisherB", 'publisher_reputation'="bad")
array('book_id'=>1, 'book_name'=>"BookA", 'author_id'=>2,
'author_name'=>"AuthorB", 'publisher_id'=>1
'publisher_name'=>"PublisherA", 'publisher_reputation'=>"good"),
array('book_id'=>1, 'book_name'=>"BookA", 'author_id'=>2,
'author_name'=>"AuthorB", 'publisher_id'=>2
'publisher_name'=>"PublisherB", 'publisher_reputation'=>"bad"),
A book can have several authors and several publishers. An author can be associated with several books. A publisher can be associated with several books. A publisher can only have one reputation (from list of values: "good","average","bad").
EDIT: The data are stored in 3 tables. author (author_id, author_name), book (book_id, book_name), publisher (publisher_id, publisher_name, publisher_reputation).
I would like to create an HTML table with the following columns:
- Book Name - Field lists specific book.
- Author(s) - Field lists all authors associated with the book.
- Publisher & Reputation - Field lists all publishers (and their reputations) associated with this book.
I am currently using the following code:
<?php function transform($books) {
$result = array();
foreach($books as $key => $book) {
$book_id = $book['book_id'];
if (!isset($result[$book_id])) {
$result[$book_id] = array(
'author_name' => array(),
'publisher_name'.'publisher_reputation' => array(),
'book_name' => $book['book_name']
);
}
$result[$book_id]['publisher_name'.'publisher_reputation'] =
array_merge($result[$book_id]['publisher_name'.'publisher_reputation'],
array(($book['publisher_name'].'('.$book['publisher_reputation'].')')));
$result[$book_id]['author_name'] = array_merge($result[$book_id]
['author_name'], array(($book['author_name'])));
}
foreach($result as $key => $data) {
$result[$key]['publisher_name'.'publisher_reputation'] =
array_unique($result[$key]['publisher_name'.'publisher_reputation']);
$result[$key]['author_name'] = array_unique($result[$key]['author_name']);
}
return $result;
}
$books = transform($books)
;?>
My html code to create the table:
<table>
<tr>
<th>Book Name</th>
<th>Author(s)</th>
<th>Publisher(s) & Reputation(s)</th>
</tr>
<?php foreach ($books as $book): ?>
<tr>
<td><?php htmlout($book['book_name']); ?></td>
<td><?php foreach(($book['author_name']) as $author_name): ?>
<?php htmlout($author_name);?><br /><?php endforeach; ?></td>
<td><?php foreach(($book['publisher_name'.'publisher_reputation']) as
$publisher_namereputation): ?></td>
<?php htmlout($publisher_namereputation);?><br /><?php endforeach; ?></td>
</tr>
<?php endforeach; ?>
Resulting table (Table 1.):
Book Name | Author(s) | Publisher(s) & Reputation(s)
BookA AuthorA PublisherA(good)
AuthorB PublisherB(bad)
- As desired: When I query "all books by AuthorA (WHERE author_id=1)," I get the above result.
- As desired: When I query "all books published by PublisherA (WHERE publisher_id=1)," I get the above result.
- Problem: When I query "all books published by a publisher with reputation = good (WHERE publisher_reputation = "good")," I get the following result:
Table 2.:
Book Name | Author(s) | Publisher(s) & Reputation(s)
BookA AuthorA PublisherA(good)
AuthorB
PublisherB, who is also associated with the very same book(but has a "bad" reputation) is not displayed. I do understand that this is because the reputation was chosen from a list ("good", "average", "bad") whose values are not associated with any individual publisher_id (several publishers can have the same reputation). I tried to solve the problem by preparing the reputation list in the following way:
try
{
$result = $pdo->query('SELECT publisher_id, publisher_reputation FROM publisher');
}
catch (PDOException $e)
{
$error = 'Error fetching publisher reputation information!';
include 'error.html.php';
exit();
}
foreach ($result as $row)
{
$reputations[] = array('publisher_id' => $row['publisher_id'],
'publisher_reputation' => $row['publisher_reputation']);
}
(...)
However, this gives me a list of reputations with repeated values (when several publishers with the same reputation are in the database).
Question: How can I design a query that returns Table 1 when I search for all book_names that are associated with a publisher_name with publisher_reputation = good? Any help would be appreciated.