0

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:

  1. Book Name - Field lists specific book.
  2. Author(s) - Field lists all authors associated with the book.
  3. 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.

4

2 回答 2

1

我没有得到完整的视图。当我环顾您发布的任何内容时,我遇到了您错过的事情,以关闭 "$result = $pdo->query(...." 行中的括号

于 2012-12-20T11:11:40.973 回答
0

我假设您的数据库中有 3 个表,它们是 -
1)书籍 -> 存储书籍相关信息,外键为 publisherId
2)出版商 -> 存储出版商相关信息,其中 publisherId 为 PK
3)声誉 -> 一种 HREF 表 b /w 将两个表 PK 作为 FK 的书籍和出版商。

如果我猜对了,您可以考虑以下查询-

SELECT * FROM books as B INNER JOIN publisher as P on P.publisherId = B.publisherId INNER JOIN 声誉为 R on (R.publisherId = B.publisherId AND R.publisherId = P.publisherId) WHERE R.Reputation ="GOOD"

于 2012-12-20T11:20:06.193 回答