如何显示与HTML中相同值的php行?

0ve6wy6x  于 2023-04-04  发布在  PHP
关注(0)|答案(1)|浏览(156)

从数据库中获取具有相同值的多个数据。我如何构造将作为子类别工作的这个。我如何将具有相同名称的所有行合并为一个?

该网站应该只显示一个潘邦加。这应该作为一个子类别工作。
下面是这个特定页面的代码。

<section class="page-section bg-dark" id="home">
    <div class="container">
        <h2 class="text-center">Tour Packages</h2>
    <div class="d-flex w-100 justify-content-center">
        <hr class="border-warning" style="border:3px solid" width="15%">
    </div>
    <div class="d-flex w-100">
        <?php
        $packages = $conn->query("SELECT * FROM `packages` order by rand() ");
            while($row = $packages->fetch_assoc() ):
                $cover='';
                if(is_dir(base_app.'uploads/package_'.$row['id'])){
                    $img = scandir(base_app.'uploads/package_'.$row['id']);
                    $k = array_search('.',$img);
                    if($k !== false)
                        unset($img[$k]);
                    $k = array_search('..',$img);
                    if($k !== false)
                        unset($img[$k]);
                    $cover = isset($img[2]) ? 'uploads/package_'.$row['id'].'/'.$img[2] : "";
                }
                $row['description'] = strip_tags(stripslashes(html_entity_decode($row['description'])));
        ?>
            <div class="card w-100 rounded-0">
                <img class="card-img-top" src="<?php echo validate_image($cover) ?>" alt="<?php echo $row['title'] ?>" height="200rem" style="object-fit:cover">
                <div class="card-body">
                <h5 class="card-title truncate-1"><?php echo $row['title'] ?></h5>
                <p class="card-text truncate"><?php echo $row['description'] ?></p>
                <div class="w-100 d-flex justify-content-end">
                    <a href="./?page=packages&id=<?php echo md5($row['id']) ?>" class="btn btn-sm btn-flat btn-warning">View Package <i class="fa fa-arrow-right"></i></a>
                </div>
                </div>
            </div>
        <?php endwhile; ?>
    </div>
    <div class="d-flex w-100 justify-content-end">
        <a href="./?page=packages" class="btn btn-flat btn-warning mr-4">Explore Package <i class="fa fa-arrow-right"></i></a>
    </div>
    </div>
</section>
pcww981p

pcww981p1#

如果您只需要每个旅游地点的一个示例,则可以在请求中使用group by语句。

SELECT tour_location FROM `your_table_name` GROUP BY tour_location;

查看更多关于https://sql.sh/cours/group-by
另一种解决方案是使用sql DISTINCT。

SELECT DISTINCT tour_location FROM `your_table_name`

相关问题