【问题标题】:How can i stop the php from showing the book twice if it has more than one author [duplicate]如果它有多个作者,我如何阻止 php 两次显示这本书 [重复]
【发布时间】:2014-04-22 20:59:00
【问题描述】:

PHP

$sql =  "SELECT DISTINCT bk.title AS Title, bk.year AS Year, bk.publisher AS Publisher, aut.authorname AS Author 
         FROM book bk 

         JOIN book_category bk_cat 
         ON bk_cat.book_id = bk.bookid

         JOIN categories cat 
         ON cat.id = bk_cat.category_id

         JOIN books_authors bk_aut 
         ON bk_aut.book_id = bk.bookid

         JOIN authors aut
         ON aut.id = bk_aut.author_id";

if (isset($_GET['searchInput'])){

    $getters = array();
    $queries = array();

    foreach ($_GET as $key => $value) {
        $temp = is_array($value) ? $value : trim($value);
        if (!empty($temp)){
        if (!in_array($key, $getters)){
            $getters[$key] = $value;
            }
        }
    }

    if (!empty($getters)) {

        foreach($getters as $key => $value){
            ${$key} = $value;
            switch ($key) {
                case 'searchInput':
                    array_push($queries,"(bk.title LIKE '%$searchInput%' 
                    || bk.description LIKE '%$searchInput%' || bk.isbn LIKE '%$searchInput%' 
                    || bk.keywords LIKE '%$searchInput%' || aut.authorname LIKE '%$searchInput%')");
                break;
                case 'srch_publisher':
                    array_push($queries, "(bk.publisher = $srch_publisher)");
                break;
                case 'srch_author':
                    array_push($queries, "(bk_aut.author_id = $srch_author)");
                break;          
        }
    }
}

if(!empty($queries)){
    $sql .= " WHERE ";
    $i = 1;
    foreach ($queries as $query) {
        if($i < count($queries)){
            $sql .= $query." AND ";
        } else {
            $sql .= $query;
        }   
        $i++;
    }
}
$sql .= " ORDER BY bk.title ASC";

}

HTML

<?php if($tot_rows > 0) { ?>
            <table id="tbl_repeat">
                <tr>
                    <th scope="col" class="bookTitle">Book Title</th>
                    <th scope="col">Author</th>
                    <th scope="col">Publisher</th>
                </tr>
                <?php do{ ?>
                <tr>
                    <td class="bookTitle"><a href=""><?php echo $rows['Title']; ?></a></td>
                    <td><?php echo $rows['Author']; ?></td>
                    <td><?php echo $rows['Publisher']; ?></td>
                </tr>
                <?php } while ($rows = mysql_fetch_assoc($rs)); ?>
            </table>
            <?php } 
            else {
                if (!empty($queries)){
                    echo "<p>There are no records matching your search criteria.</p>";
                } else {
                    echo "<p>There are currently no records available.</p>";
                }
            }
            ?>
    </div>

从图片中可以看出,这本书两次显示了两位作者,因为你知道书籍可以有很多作者,所以我试图弄清楚它如何只显示与那本书相关的一个或第一个.

【问题讨论】:

  • You already asked the same question如果我错了请纠正我
  • @Fred-ii- 是的,我真的很茫然:(对不起
  • 给你an answer你没试过吗?有人赞成这个答案,所以它一定有一些东西可以解决你的问题或引导你走向正确的方向。
  • @Fred-ii- 是的,当然我试过它并没有改变结果
  • 您当时应该对给您答案的人发表评论。 “不”说什么是没有意义的。那个人怎么知道它是否有效?你需要互动;-)

标签: php html mysql sql phpmyadmin


【解决方案1】:

我相信这可以使用 GROUP_CONCAT 来实现。查看this 教程以了解它。编辑,this 可能是更好的解释。

所以我猜应该是:

    SELECT DISTINCT bk.title AS Title, bk.year AS Year, bk.publisher AS Publisher, GROUP_CONCAT(aut.authorname) AS Author 
         FROM book bk 

         JOIN book_category bk_cat 
         ON bk_cat.book_id = bk.bookid

         JOIN categories cat 
         ON cat.id = bk_cat.category_id

         JOIN books_authors bk_aut 
         ON bk_aut.book_id = bk.bookid

         JOIN authors aut
         ON aut.id = bk_aut.author_id
GROUP BY bk.title, bk.year, bk.pulisher

【讨论】:

  • Another Happy Ending 然后。
猜你喜欢
  • 1970-01-01
  • 2021-08-16
  • 2014-06-26
  • 1970-01-01
  • 2015-06-16
  • 1970-01-01
  • 1970-01-01
  • 2014-07-06
  • 2012-08-10
相关资源
最近更新 更多