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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。