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条)

报告相同问题?

悬赏问题

  • ¥30 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页