简体   繁体   中英

PHP display image based on positive or negative number

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> echo "<td>" . $row['']. "</td> 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.

您没有告诉我们$myNumber来源,因此如果变量名不同,只需在下面的代码中对其进行修改。

echo '<td>/images/image' . ($myNumber == 0 ? '3' : ($myNumber >= 1 ? '1' : '2')) . '.jpg</td>';

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

Try:

if($myNumber < 0) {
    //show /images/image2.jpg
}
if($myNumber == 0) {
    //show /images/image3
}
if($myNumber > 0) {
    //show /images/image1.jmp
}

In your code:

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>";
    if($row['percentchange'] < 0) {
      //show /images/image2.jpg
    }
    if($row['percentchange'] == 0) {
      //show /images/image3
    }
    if($row['percentchange'] > 0) {
      //show /images/image1.jmp
    }
  echo "</td>";

}

This is assuming, based off of your other comments, that $row['percentchange'] is the number in particular that you care about checking against.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM