简体   繁体   中英

MySQL output in PHP table

I try to get data out of MySQL and show it in a PHP table but the sum_order is showing only in one row and all the other data are good. I don't find where did I go wrong with it.

Table structure

be table

barcode amount date user
R-001 10 2022-01-03 10:41:24 38

brikett_order table

barcode amount date costumer state
R-001 10 2022-01-03 10:41:24 corp zárt

My code:

<div class="col-sm">
            <h5 class="mt-3">Havi Gyártás</h5>
            <table class="table">
                <thead>
                    <tr>
                    <th scope="col">Hónap</th>
                    <th scope="col">Munkanapok</th>
                    <th scope="col">Gyártás</th>
                    <th scope="col">Eladás</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $sql_list = 
                    "SELECT MONTH(date) AS month,   
                    COUNT(DISTINCT DATE(date)) AS work_days, 
                    COUNT(*) AS sum_number
                    FROM be 
                    WHERE barcode='R-001'
                    GROUP BY month";
                    $result_list = mysqli_query($conn, $sql_list);
                    while($row = mysqli_fetch_assoc($result_list)) {  
                        ?>
                    <tr>
                    <th scope="row"><?=$row['month']?></th>
                    <td><?=$row['work_days']?></td>
                    <td><?=$row['sum_number']?>&nbsp;db</td>
                    <?php
                    }
                    ?>
                    <?php
                    $sql_list = 
                    "SELECT MONTH(date) AS month,   
                    COUNT(DISTINCT DATE(date)) AS work_days, 
                    SUM(amount) AS sum_order
                    FROM brikett_order 
                    WHERE barcode='R-001' AND state='zárt'
                    GROUP BY month";
                    $result_list = mysqli_query($conn, $sql_list);
                    while($row = mysqli_fetch_assoc($result_list)) {  
                        ?>
                    <td><?=$row['sum_order']?>&nbsp;db</td>
                    <?php
                    }
                    ?>
                </tr>
                </tbody>
            </table>
        </div>

The output table is like this:

month work_days sum_number sum_order
1 29 338
2 10 150 233 100

What I'am looking for:

month work_days sum_number sum_order
1 29 338 233
2 10 150 100

It is hard to predict table links without table structure, but you could implement all in one query using JOIN or LEFT JOIN

SELECT MONTH(be.date) AS month,
COUNT(DISTINCT DATE(be.date)) AS work_days,
COUNT(be.date) AS sum_number,
SUM(bo.amount) AS sum_order 
FROM be
LEFT JOIN brikett_order bo on bo.state='zárt' and bo.barcode =  be.barcode and bo.date = be.date
WHERE be.barcode='R-001'
GROUP BY month

Then just create table rows at once:

 <tr>
   <th scope="row"><?=$row['month']?></th>
   <td><?=$row['work_days']?></td>
   <td><?=$row['sum_number']?>&nbsp;db</td>
   <td><?=$row['sum_order']?>&nbsp;db</td>
 </tr>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM