亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將列中的信息拉到 for 循環內

將列中的信息拉到 for 循環內

PHP
皈依舞 2023-04-28 17:18:23
我有一個 Orders 項目,我想在其中實現標簽打印。每個訂單必須根據其物品數量顯示標簽數量。到目前為止,我已經設法用“for”來顯示數量。數據庫示例:+-------+-------------+-----+| order | description | qty |+-------+-------------+-----+|   1   |   stickers  |  1  ||   2   |   posters   |  2  ||   2   |   tape      |  1  ||   2   |  (no desc)  |  1  |+-------+-------------+-----+到目前為止我所擁有的:Select SUM(qty) AS pieces FROM mytable WHERE order = '2'");    $total=$row['pieces']; //this correspond to all items on the order.for ($x = 1; $x <= $row_count; $x++){  echo $order."<br>"; //order number  echo $x."<br>"; //item  echo $total."<br>"; //total items}結果:           #order   #item   #total        #order   #item   #total          +-------+-------+-------+      +-------+-------+-------+posters   |   2   |   1   |   4   |      |   2   |   2   |   4   |          +-------+--- ---+-------+      +-------+-------+-------+        #order   #item   #total       +-------+-------+-------+tape   |   2   |   3   |   4   |       +-------+-------+-------+            #order   #item   #total           +-------+-------+-------+(no desc)  |   2   |   4   |   4   |           +-------+-------+-------+但是我無法顯示每個“描述”欄的內容。理想的結果:         #order  #desc #item #total    #order  #desc #item #total          +---+--------+----+----+      +---+--------+----+----+posters   | 2 | posters|  1 |  4 |      | 2 | posters|  2 |  4 |          +---+--------+----+----+      +---+--------+----+----+      #order  #desc #item #total       +---+--------+----+----+tape   | 2 |  tape  |  3 |  4 |       +---+--------+----+----+          #order  #desc #item #total           +---+---------+----+----+(no desc)  | 2 |(no desc)|  4 |  4 |           +---+---------+----+----+有機會得到這個結果嗎?我正在使用 php 和 mysql。
查看完整描述

1 回答

?
慕妹3146593

TA貢獻1820條經驗 獲得超9個贊

您可以將總查詢與表本身的查詢結合起來。


SELECT m.description, m.qty, t.total

FROM mytable AS m

CROSS JOIN (

    SELECT SUM(qty) AS total

    FROM mytable

    WHERE order = 2

) AS t

WHERE order = 2

然后你可以使用嵌套循環:


$j = 1;

while ($row = $stmt->fetch()) {

    echo $row['description'];

    for ($i = 1; $i <= $row['qty']; $i++, $j++) {

        echo "$order<br>{$row['description']}<br>$j<br>{$row['total']}<br>";

    }

}


查看完整回答
反對 回復 2023-04-28
  • 1 回答
  • 0 關注
  • 109 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號