【问题标题】:How to Highlight smallest value from 4 columns? mysql + PHP如何突出显示 4 列中的最小值? mysql + PHP
【发布时间】:2013-09-30 22:12:44
【问题描述】:
$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() 函数,类似于: 最低(价格1,价格2,价格3,价格4)

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";

}

添加 else 是否正确?

2013 年 10 月 10 日更新 - 开始工作了(有点)...

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

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

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    您应该在查询中使用 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
        }
    }
    

    【讨论】:

    • 您好 Jan,我已尝试实施您的答案,但我的结果都显示为红色,-我再次发表评论。,
    • 您能否将您的价格列修改为不为空且默认值 = 0.00?例如像这样:ALTER TABLE pricing CHANGE price1 price1 DECIMAL( 12, 2 ) NOT NULL DEFAULT '0.00'
    • @CraigDearden 现在对你有用吗?如果你接受我的回答就好了
    【解决方案2】:

    您可以使用sort 函数将它们从low to high 中排序。

    $values = sort(array($row['Price1'], $row['Price2'], $row['Price3'], $row['Price4'], SORT_NUMERIC));
    
    echo $values[0]; //lowest price
    

    【讨论】:

      【解决方案3】:

      使用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";
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-02
        • 2022-01-11
        • 2019-12-12
        • 1970-01-01
        • 2022-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多