如何在PHP中查询相关数组中的值,并将其显示在HTML表中



下面的二维数组存储图书(1)的信息。ID, 2。的名字,3。作者ID, 4。作者名;出版商ID, 6。出版商名称;出版商声誉):

$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"),

一本书可以有几个作者和几个出版商。一个作者可以与几本书联系在一起。一个出版商可以与几本书相关联。一个出版商只能有一个声誉(从价值列表中:"好"、"一般"、"坏")。

EDIT:数据存储在3个表中。作者(author_id, author_name),图书(book_id, book_name),出版商(publisher_id, publisher_name, publisher_reputation).

我想创建一个包含以下列的HTML表:

  1. 书名-字段列出特定的书。
  2. 作者-字段列出与该书相关的所有作者。
  3. 出版商,声誉-字段列出所有出版商(和他们的声誉)与这本书。

我目前正在使用以下代码:

<?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)
;?>  
创建表的html代码:
<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; ?>

结果表(表1):

Book Name       | Author(s)        | Publisher(s) & Reputation(s)
BookA             AuthorA            PublisherA(good)
                  AuthorB            PublisherB(bad)
  • 如预期:当我查询"所有书籍由AuthorA (WHERE author_id=1)"时,我得到了上面的结果。
  • 如所愿:当我查询"PublisherA (WHERE publisher_id=1)出版的所有书籍"时,我得到上述结果。
  • 问题:当我查询"出版商出版的所有书籍与声誉=好(WHERE publisher_reputation ="好"),"我得到以下结果:
表2

Book Name       | Author(s)        | Publisher(s) & Reputation(s)
BookA             AuthorA            PublisherA(good)
                  AuthorB            

PublisherB,也与同一本书有关联(但有一个"坏"名声),没有显示。我确实理解这是因为声誉是从列表("好","平均","坏")中选择的,其值与任何单独的publisher_id(几个出版商可以有相同的声誉)无关。为了解决这个问题,我试着用下面的方法来制作信誉列表:

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']);
}
(...) 

然而,这给了我一个具有重复值的声誉列表(当数据库中有几个具有相同声誉的出版商时)。

问题:当我搜索与publisher_reputation = good的publisher__name关联的所有book_names时,我如何设计一个返回表1的查询?任何帮助都会很感激。

我没有得到完整的视图。当我环顾你张贴的任何东西时,我遇到了一件事,你错过了在"$result = $pdo->query(....)"行关闭一个括号。

我假设你的数据库中有3个表-
1) Books ->存储图书相关信息,外键为publisherId
2) Publisher ->存储Publisher相关信息,其中publisherId为PK
3)声望->类HREF表b/w图书和出版商,两个表PK为FK。

如果我猜对了,那么你可以考虑以下查询-

SELECT * FROM books as BINNER JOIN publisher作为P on p.p publisherid = b.p publisheridINNER JOIN声誉为R on (r.p publisherid = b.p publisherid AND r.p publisherid = .p . publisherid) WHERE R.Reputation ="GOOD"

相关内容

  • 没有找到相关文章

最新更新