我有這段代碼,可以從數(shù)據(jù)庫(kù)獲取數(shù)據(jù)并插入到數(shù)組中,也可以將其插入同一數(shù)據(jù)庫(kù)中的另一個(gè)表中,但我想要的是數(shù)據(jù)中有一個(gè)價(jià)格,如果有可用的數(shù)據(jù)可以是一個(gè)或兩個(gè),并且將其加在一起時(shí)的價(jià)格大于特定值,然后執(zhí)行某些操作<?phpsession_start();require "../db/dbconn.php";$sql = "SELECT cartPrice FROM cart";$result = mysqli_query($conn, $sql);$datas = array();if(mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_assoc($result)) { $datas[] = $row; }}foreach($datas as $data) { $price = $data['cartPrice']; #so how do i do it if $price is greater than maybe 1000 then do this especially when it loops more than one $stmt = $conn->prepare("INSERT INTO order (price) VALUES (?)"); $stmt->bind_param("s", $price); $stmt->execute();}有誰(shuí)知道我該怎么做,如果你不明白,我仍然可以向你解釋
1 回答

慕的地10843
TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超8個(gè)贊
我從你的問(wèn)題中了解到的是,你想要計(jì)算 $datas 數(shù)組中的價(jià)格總和,并對(duì)該價(jià)格總和應(yīng)用一些條件。好吧,您可以簡(jiǎn)單地創(chuàng)建一個(gè)變量并繼續(xù)在其中添加 $price 。然后在循環(huán)結(jié)束時(shí)應(yīng)用您的條件。
$totalPrice = 0;
foreach($datas as $data) {
$price = $data['cartPrice'];
$totalPrice = $totalPrice + $price
$stmt = $conn->prepare("INSERT INTO order (price) VALUES (?)");
$stmt->bind_param("s", $price);
$stmt->execute();
}
if($totalPrice > 1000) {
# DO SOMETHING
}
- 1 回答
- 0 關(guān)注
- 129 瀏覽
添加回答
舉報(bào)
0/150
提交
取消