dqyitt2954 2018-06-14 14:51
浏览 29
已采纳

PHP根据正数或负数显示图像

I need to display:

/images/image1.jmp if the number is positive

or

/images/image2.jpg if the number is negative

or

/images/image3 if the number is 0.

$stmt = sqlsrv_query($conn,$sql);
    echo "<table border='1'><tr><th>Offense</th><th>Previous Date Range</th><th>Current Date Range</th><th>Difference</th><th>Percentage Difference</><th>Up or down image in this column</></tr>";

while( $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) ) 
{
    echo "<tr>";
    echo "<td>" . $row['Offense']. "</td>";
    echo "<td>" . $row['PreviousDateRange']."</td>";
    echo "<td>" . $row['DateRange']."</td>";
    echo "<td>" . $row['difference1']."</td>";
    echo "<td>" . $row['percentchange']."</td>";
    echo "<td>" . $row['']. "</td>";
    }
    echo "</table>";         

 ?>

I found this code and have tried different ways if incorporating it in the echo "<td>" . $row['']. "</td> but not having any luck.

I get the code but can not manipulate it to fit what it needs to do. I'm sure it's a simple solution. Just frustrated.

switch ($myNumber) {
  case 0:
    echo "Zero is not a valid value.";
    break;
  case $myNumber < 0:
    echo "Negative numbers are not allowed.";
    break;
  default:
    echo "Great! Ready to make calculations.";
    break;
}

Thanks for the help guys. Got the answer that worked.

  • 写回答

3条回答 默认 最新

  • doujingke4981 2018-06-14 15:17
    关注

    I think this is a more readable solution.
    I use an array to hold the links and use a calculation to see if it is negative or positive.

    $img = ["-1" => "/images/image2.jpg", 
    "0" => "/images/image3.jpg",
    "1" => "/images/image1.jpg"];
    
    $number = 0;
    
    Echo ($number == 0 ? $img[$number] : $img[$number/abs($number)]);
    

    https://3v4l.org/JstZl

    If the number is positive the calculation will be 15/15 => 1.
    If the number is negative -10/10 => -1.

    Edit:

    $stmt = sqlsrv_query($conn,$sql);
    echo "<table border='1'><tr><th>Offense</th><th>Previous Date Range</th><th>Current Date Range</th><th>Difference</th><th>Percentage Difference</><th>Up or down image in this column</></tr>";
    $img = ["-1" => "/images/image2.jpg", 
    "0" => "/images/image3.jpg",
    "1" => "/images/image1.jpg"];
    
    
    while( $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) ) 
    {
        echo "<tr>";
        echo "<td>" . $row['Offense']. "</td>";
        echo "<td>" . $row['PreviousDateRange']."</td>";
        echo "<td>" . $row['DateRange']."</td>";
        echo "<td>" . $row['difference1']."</td>";
        echo "<td>" . $row['percentchange']."</td>";
        Echo '<td><img src="' . ($row['percentchange'] == 0 ? $img[$row['percentchange']] : $img[$row['percentchange']/abs($row['percentchange'])]) . '"></td>';
    }
    echo "</table>";         
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭