Enric Agud Pique Enric Agud Pique -4 years ago 121
PHP Question

PHP elseif else

With the following code, I would like to plot the image sun.png when sky is clear, cloud when the sky is cloudy and variable in other cases...but something fails...I always get the image variable.png

<?php
if($sky == "clear" ) {
echo '<img src="images/sun.png" width="40">';
}
elseif ($sky == "cloudy" ){
echo '<img src="images/cloud.png" width="40">';
}
else {
echo '<img src="images/variable.png" width="40">';
}
?>


I consult the database using this code @Jack Goodman

$data_query = mysqli_query($conexionbd,'select * from `weather` where `data` = "2017-03-22" and (`num` = "1" or `num` = "2" or `num` = "3")');
while($data = mysqli_fetch_assoc($data_query)){ ?>


At the end I have solved it, my code had a mistake, the right code is

<?php
if($data['sky'] == "clear" ) {
echo '<img src="images/sun.png" width="40">';
}
elseif ($data['sky'] == "cloudy" ){
echo '<img src="images/cloud.png" width="40">';
}
else {
echo '<img src="images/variable.png" width="40">';
}
?>

Answer Source

Your problem is somewhere in the variable sky. I downloaded pictures and tested it with this code by changing the variable's value and it all works fine.

$sky = "cloudy";

if($sky == "clear" ) 
{
echo '<img src="sun.jpg" width="40">';
} 
elseif ($sky == "cloudy" ) 
{
echo '<img src="cloud.jpg" width="40">';
} 
else 
{
echo '<img src="variable.png" width="40">';
}

Show me the code where you get the sky's value please.

Sunny

Cloudy

Variable

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download