gpt4 book ai didi

php - 从数据库获取所有结果,但显示最低价格为 0 价格

转载 作者:行者123 更新时间:2023-11-29 23:07:04 25 4
gpt4 key购买 nike

我有一个餐厅脚本,您可以在购物车中以 1+1 报价添加披萨,我希望如果有 2 个披萨,则显示最贵的披萨的实际价格和最便宜的 0.01 的披萨,如果例如,用户添加了 3 个售价 10 美元的披萨和 3 个售价 11 美元的披萨,需要为每个售价 11 美元的披萨显示 11 美元,并为 3 个售价 10 美元的披萨显示 0.01,我尝试了一切,但无法弄清楚这一点。/p>

$dbres1plus1 = mysql_query("SELECT * FROM cos_cumparaturi WHERE extra='1plus1' AND ip='$user_ip'");
$numara_pizze = mysql_num_rows($dbres1plus1);
if($numara_pizze % 2 != 0) {

echo '<tr>

<td colspan="5"><sup style="color: red;"><strong>Ai adaugat '.$numara_pizze.' pizze in oferta, ai nevoie de '.$numara_pizze_total.' pizze pentru a completa oferta.</strong></sup></td>
</tr>';
}
}
echo '<tr id="'.$cos{'id'}.'">
<td colspan="3"><strong>'.$pizza{'nume'}.'</strong> <em>('.$marime.'cm)</em></td>
<td align="right" nowrap="nowrap" style="text-align:right;">'.$pizza{'pret'}*$happyhour.' lei</td>
<td width="20">

<a href="#" class="stergecos" id="'.$cos{'id'}.'">

&nbsp;<img style="margin-top:4px;" src="http://website/images/delete_icon.png" widht="16" height="16" border="0" alt="Sterge produs" /></a></td>
<td>&nbsp;&nbsp;&nbsp;</td></tr>';
}
}

我在数据库中有一行包含价格的行。我试图获得最低的数字并替换它,但由于它是一个 while 循环,所以它替换了所有价格。

最佳答案

最简单的方法是使用MYSQL中的ORDER BY。如果您按 PRICE DESC 订购,则可以显示前半部分正确,后半部分显示为 0.01

简单的例子:

<?php
$query = 'SELECT `Pizza`, `Price` FROM `orders` ORDER BY `Price` DESC';

//your code to exec query

//get the row count (example with mysqli)
$nrOfOrders = $result->num_rows;

//1 = pay 1
//2 = pay 1;
//3 = pay 2;
//4 = pay 2;
//etc
$fullPrice = ceil($nrOfOrders/2);

$index = 1;
//loop trough orders
foreach($result as $row)
if ($index <= $fullPrice) {
//show full price
} else {
//show 0.01
}

//increase index
$index++;
}

?>

关于php - 从数据库获取所有结果,但显示最低价格为 0 价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28256805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com