PHP[将数据库中的数据排序为可折叠的bootstrap 4卡][重复]。

问题描述 投票:0回答:1

正如标题所说,我正在为一个网站开发购物车,购物车中包含了所有的产品,从不同的公司订购这样的:-。截图

所以,正如你所看到的,我试图排序的所有产品,是来自同一公司的下面彼此作为特定的公司法案我的问题,我怎么能完成?

我已经试过了:-没有什么可说的其实我真的很困惑,我现在不知道我要(循环或类似的东西...)我希望我解释了我想要完成的,如果没有()。截图)代码:- php PDOquery

<?
$accountid = '8';
require_once '..\Config.php';
 // WHERE chemicalcom='$variable' OR name='$variable'
$dbCon = "mysql:host=$host;dbname=$db_name";
$variable = "Efexor";
$PDOCon = new PDO($dbCon, $username, $password);
$query = $PDOCon->prepare("SELECT * FROM basket WHERE accountid ='$accountid'");
$query->execute();
$basketItems = $query->fetchAll();
?>

代码:-index.php

        <? foreach($basketItems as $item){
        echo'<h3>Bill('.$item['companyexported'].')</h3>
        <div class="card">
        <div class="row">
            <div class="col-6"><img src="'.$item['imgpath'].'" class="productimg" alt=""></div>
            <div class="col-auto">
                <div class="card-title">
                    <div class="row">'.$item['name'].'</div>
                    <div class="row">'.$item['chemicalcom'].'</div>
                    <div class="row">'.$item['concentration'].'</div>
                    <br>
                    <div class="row">'.$item['price'].' $
                    </div>
                    <span class="badge badge-info qty">'.$item['qty'].'</span>
                </div>
            </div>
        </div>
    </div>';}?>

谢谢。

php html pdo bootstrap-4
1个回答
0
投票

首先,你需要更新你的sql到按公司排序,这样会使php更简单。

SELECT * FROM basket WHERE accountid ='$accountid' order by companyexported

然后,在你的php中,你只想显示公司一次,所以你创建一个空变量并检查它。如果该变量不包含匹配的公司名称,则显示公司,并将该变量设置为当前公司名称。

        <?
    $curr_co = "";

foreach($basketItems as $item){
    if($curr_co != $item['companyexported']){
           echo '<h3>Bill('.$item['companyexported'].')</h3>';
           $curr_co = $item['companyexported'];
    }
           echo '<div class="card">
                <div class="row">
                    <div class="col-6"><img src="'.$item['imgpath'].'" class="productimg" alt=""></div>
                    <div class="col-auto">
                        <div class="card-title">
                            <div class="row">'.$item['name'].'</div>
                            <div class="row">'.$item['chemicalcom'].'</div>
                            <div class="row">'.$item['concentration'].'</div>
                            <br>
                            <div class="row">'.$item['price'].' $
                            </div>
                            <span class="badge badge-info qty">'.$item['qty'].'</span>
                        </div>
                    </div>
                </div>
            </div>';}?>
© www.soinside.com 2019 - 2024. All rights reserved.