ThinkPHP? ??? ??? ?? ??? ??? ???? ?? ???? ?? ???? ? ?? ?? PHP ?? ????????. ThinkPHP ?????? ??? ??? ?? ???? ?? ???? ?? ? ?? ??? ??? ??? ??? ?? ??? ? ????.
ThinkPHP ??????? ?? ??? ????? ????? ?????. ?? ?? ?? ??? ???? ? ?? ??? ???? ?? ?? ?? ??? ??? ? ????. ???? ThinkPHP ????? ?? ??? ???? ??? ?????.
?? PHP ???? ???? ?? ???? ??? ?? ?? ???? ???? ???? ???. ?? ???? ? ?? ??? ???? ??? ???? ?? ??? ??? ? ????.
//?獲取訂單列表 $orderList?=?Db::name('order')->select(); //?計算訂單總金額 $totalAmount?=?0; foreach?($orderList?as?$order)?{ ????$totalAmount?+=?$order['amount']; } //?將數(shù)據(jù)傳遞給模板 $this->assign('orderList',?$orderList); $this->assign('totalAmount',?$totalAmount);
? ????? ?? Db::name('order')->select()? ?????.
?? ?? ???? ?? ? foreach
??? ???? ?? ??? ???? ? ?? ??? ???? ????? ?? ??? ???? ??????. ?? ?? ?? ??? ?? ??? ?? ???? ?????. Db::name('order')->select()
方法獲取訂單列表數(shù)據(jù),然后使用foreach
循環(huán)對訂單列表進行遍歷,累加每個訂單的金額,最后得到訂單的總金額。然后將訂單列表和訂單總金額都傳遞給模板。
接下來,在模板中進行求和操作。在ThinkPHP模板中,我們可以使用volist
標簽對數(shù)據(jù)進行遍歷,然后使用sum
屬性對指定的字段進行求和操作,代碼如下:
<!-- 訂單列表 --> <table> ????<thead> ????????<tr> ????????????<th>訂單編號</th> ????????????<th>訂單金額</th> ????????</tr> ????</thead> ????<tbody> ????????<!-- 遍歷訂單列表 --> ????????<volist name="orderList" id="order"> ????????????<tr> ????????????????<td>{$order.order_no}</td> ????????????????<td>{$order.amount}</td> ????????????</tr> ????????</volist> ????????<!-- 計算總金額 --> ????????<tr> ????????????<td>總金額:</td> ????????????<td>{$orderList|sum='amount'}</td> ????????</tr> ????</tbody> </table>
在上述代碼中,我們使用volist
標簽對訂單列表進行遍歷,然后將訂單編號和訂單金額分別顯示在表格中。在表格的底部,我們使用sum
屬性對orderList
變量中的amount
字段進行求和操作,這樣就可以得到訂單的總金額了。
總結(jié):在ThinkPHP中進行模板求和操作非常簡單,只需要在PHP代碼中計算好數(shù)據(jù),然后將數(shù)據(jù)傳遞給模板,使用sum
volist
??? ???? ???? ??? ?? sum
??? ?????? ??? ??? ?? ?? ??? ??? ? ????. ??rrreee???? ????? volist
??? ???? ?? ??? ??? ?? ?? ???? ?? ??? ?? ??? ?????. ??? ???? sum
??? ?????? orderList
??? amount
??? ???? ??? ?? ? ????. ?? ??. ??????: ThinkPHP?? ??? ?? ??? ???? ?? ?? ?????. PHP ??? ???? ??? ?? ???? ???? ???? sum
??? ?????? ???. ?? ??? ?????. ??? ??? ThinkPHP ????? ?? ??? ???? ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)