0
$result = mysql_query("SELECT * FROM pricing ORDER BY Product ASC LIMIT 5000");

以及要遵循的 PHP

$counter = 0;
$btncounter = 0;
while($row = mysql_fetch_array($result)){
$counter=$counter+1;
$btncounter=$btncounter+1;
print "<tr>\n";
print "<td>".$row['Product']."</td>\n";
print "<td>".$row['Price1']."</td>\n";
print "<td>".$row['Price2']."</td>\n";
print "<td>".$row['Price3']."</td>\n";
print "<td>".$row['Price4']."</td></tr>\n";

我想以红色/粗体/或下划线显示最低价格,我试图弄清楚如何使用 Least() 函数,类似于:LEAST(price1, price2, price3, price4)

if($row['Price1'] == LEAST(price1, price2, price3, price4)
{print "<td>Price 1 in Red</td>\n";}

还是那条路线上的东西?

谁能指出我正确的方向?

Jan - 我试图利用你的答案:但我的结果都显示为红色,我的 mysql 查询和你的一样。

while($row = mysql_fetch_array($result))
{
$counter=$counter+1;
$btncounter=$btncounter+1;
print "<tr>\n";
print "<td>".$row['ID']."</td>\n";
print "<td>".$row['Product']."</td>\n";
if ($result['price1'] == $result['smallest_price']) { print "<td><span style='color:red'>".$row['price1']."</span></td>\n"; } else {print "<td>".$row['price1']."</td>\n";}
if ($result['price2'] == $result['smallest_price']) { print "<td><span style='color:red'>".$row['price2']."</span></td>\n"; } else {print "<td>".$row['price2']."</td>\n";}
if ($result['price3'] == $result['smallest_price']) { print "<td><span style='color:red'>".$row['price3']."</span></td>\n"; } else {print "<td>".$row['price3']."</td>\n";}
if ($result['price4'] == $result['smallest_price']) { print "<td><span style='color:red'>".$row['price4']."</span></td>\n"; } else {print "<td>".$row['price4']."</td>\n";}
print "<td>".$row['Last_updated']."</td>\n";

}

我添加其他是否正确?

2013 年 10 月 10 日更新 - 让它工作(有点)......

使用: if ($row['Price1'] == $row['smallest_price']) { print "".$row['Price1']."\n"; } else { print "".$row['Price1']."\n";}

但它仅在所有四个价格都有价值时才有效 - 如果价值不存在怎么办?

4

3 回答 3

3

您应该在查询中使用 Mysql 最少函数:

$result = mysql_query("SELECT *, LEAST(price1, price2, price3, price4) AS smallest_price FROM pricing ORDER BY Product ASC LIMIT 5000");

while($row = mysql_fetch_array($result)){
    if ($result['price1'] == $result['smallest_price']) {
         // print price 1 in red
    }
}
于 2013-09-30T22:22:56.323 回答
0

您可以使用sort函数对它们进行排序low to high

$values = sort(array($row['Price1'], $row['Price2'], $row['Price3'], $row['Price4'], SORT_NUMERIC));

echo $values[0]; //lowest price
于 2013-09-30T22:19:33.783 回答
0

使用http://php.net/min

$counter = 0;
$btncounter = 0;
while($row = mysql_fetch_array($result)){
$counter=$counter+1;
$btncounter=$btncounter+1;
print "<tr>\n";
print "<td>".$row['Product']."</td>\n";
$prices = array($row['Price1'],$row['Price2'],$row['Price3'],$row['Price4']);
$minimum_price = min($prices)
foreach ( $prices as $price) {
   $price_text = $price;
   if ($price == $minimum_price) {
      $price_text = "<span style='color:red'>$price_text</span>";
   }
   print "<td>".$price_text."</td>\n";
}
于 2013-09-30T22:24:14.110 回答