Author Topic: if data is between "then echo"  (Read 320 times)

0 Members and 1 Guest are viewing this topic.

Offline jacko_162Topic starter

  • Enthusiast
  • Posts: 306
  • Gender: Male
    • View Profile
    • Real-Creative.co.uk
if data is between "then echo"
« on: March 10, 2010, 08:26:40 AM »
i have a set of values that i echo on a page;

$test1, $test2, $test3 etc etc..

i want an if statement to show if the said value is between X and y show blueicon.png else show redicon.png

i have been googling syntax but to no avail.

this is my current code snippet;
<?php echo $test1 ?> <? if ($test1 < 1.025)
echo "show blue icon.png"; ?>



i love the phpfreaks :)

Offline Wolphie

  • Devotee
  • Posts: 681
  • Gender: Male
  • If you ask a question; please help answer one!
    • View Profile
Re: if data is between "then echo"
« Reply #1 on: March 10, 2010, 08:30:47 AM »
Code: [Select]
<?php
$val1 
5;
$val2 10;
$val3 7;

if ((
$val3 $val1) && ($val3 $val2)) {
  echo 
'icon.png';
}
else {
  echo 
'othericon.png';
}
?>

Science without religion is lame. Religion without science is blind.

Offline Adam

  • Guru
  • Fanatic
  • *
  • Posts: 4,702
  • Gender: Male
    • View Profile
Re: if data is between "then echo"
« Reply #2 on: March 10, 2010, 08:37:47 AM »
You could use something along the lines of:

if ($test1 $x && $test1 $y) {
    echo 
'<img src="blueicon.jpg" />';
} else {
    echo 
'<img src="redicon.jpg" />';
}


However for something like this you may be better off using the ternary operator (scroll down to example 2):

Code: [Select]
<img src="<?php echo ($test1 $x && $test1 $y) ? 'blueicon.jpg' 'redicon.jpg'?>" />
Edit: not sure given your original description and Wolphie's reply, which numbers you want to compare..
Ronnie Wood, true or false?

Offline Wolphie

  • Devotee
  • Posts: 681
  • Gender: Male
  • If you ask a question; please help answer one!
    • View Profile
Re: if data is between "then echo"
« Reply #3 on: March 10, 2010, 08:39:07 AM »
Me either really, I just took a shot in the dark.
Science without religion is lame. Religion without science is blind.

Offline jacko_162Topic starter

  • Enthusiast
  • Posts: 306
  • Gender: Male
    • View Profile
    • Real-Creative.co.uk
Re: if data is between "then echo"
« Reply #4 on: March 10, 2010, 08:45:48 AM »
Me either really, I just took a shot in the dark.

each statement will have the numbers entered manually.

for instance;

IF $test1 is between 1.020 1.027 show blueicon.png
else show redicon.png


hope this clears it up, tbh i could have these set values in the database but for now i need them in the code.
i love the phpfreaks :)

Offline Adam

  • Guru
  • Fanatic
  • *
  • Posts: 4,702
  • Gender: Male
    • View Profile
Re: if data is between "then echo"
« Reply #5 on: March 10, 2010, 08:48:35 AM »
Replacing the $x and $y vars in my example should be what you need.
Ronnie Wood, true or false?

Offline jacko_162Topic starter

  • Enthusiast
  • Posts: 306
  • Gender: Male
    • View Profile
    • Real-Creative.co.uk
Re: if data is between "then echo"
« Reply #6 on: March 10, 2010, 09:28:36 AM »
cheers guys :)

 Wolphie version seemed the better option and its the first i tried :)

problem now solved.
i love the phpfreaks :)