Help with Logic Operators

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
richardk
Forum Newbie
Posts: 4
Joined: Thu Jun 05, 2008 11:07 pm

Help with Logic Operators

Post by richardk »

I am not a programmer, as one can probably tell by this code but nerer the less.....

I am trying to display particular images on a web page based on weather conditions. I am able to display individual images using if/elseif statements but when trying to us logic operators to display multiple images for multiple conditions I get now where.

I suspect I am looking in the totally wrong direction.

Here is the code, which includes an explode() function to parse a comma delimited list, as well as a sample of the comma list. The comma list is generated by our in-house weather software and is updated every two minutes or when there is a change in a state.

I work at a small private observatory. We host scopes for hobbiests as well as for some organizations, even have one here for NASA.
The web page I am adding this script to is located at: http://www.nmskies.com/weather.html (there is also a sub domain at
http://hosting.nmskies.com)

I am trying to display images 100 X 640 in either green (open), black (closed for daylight), or red (closed for rain, wind, cloud or fog).
These images will be displayed directly under the weather data at the top of the page and will allow our customers to determine the state of the observatories at a glance.

We have 14 domes and 3 roll of roof that house about 45 scopes in all.

If you need any more information, please let me know.

Thanks for any help.
richardk

start comma list
no sun,no rain,no wind,no clouds,no fog
end of comma list

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title></title>
  <meta http-equiv="generator" content="CoffeeCup HTML Editor (http://www.coffeecup.com)" />
  <meta name="created" content="Sat, 01 Jan 2011 05:31:00 GMT" />
  <meta http-equiv="content-type" content="text/html;charset=utf-8" />
  <meta name="description" content="" />
  <meta name="keywords" content="" />
  
  <style type="text/css">
div.img
  {
  margin:2px;
  margin-left:100px;
  height:200px;
  width:640px;
  float:left;
  text-align:center;
  }
</style>

</head>
<body>
<div class = "img">

<?php
$filename = "conditions.txt";
$fd = fopen ($filename, "r");
$contents = fread ($fd, "75");
fclose ($fd);
$delimiter = ",";
$split = explode($delimiter, $contents);


if ($split[0] == "sun"){	
	echo '<img src="images/daylight.jpg?image=.\img\default.jpg&height=200&width=640" />';
	}	

elseif ($split[1] == "rain"){
	echo '<img src="images/rain.jpg?image=.\img\default.jpg&height=200&width=640" />';
	}	
	
elseif ($split[2] == "wind"){
	echo '<img src="images/wind.jpg?image=.\img\default.jpg&height=200&width=640" />';
	}

elseif ($split[3] == "clouds"){
	echo '<img src="images/clouds.jpg?image=.\img\default.jpg&height=200&width=640" />';
	}

elseif ($split[4] == "fog"){
	echo '<img src="images/fog.jpg?image=.\img\default.jpg&height=200&width=640" />';
	}
elseif ($split[1] == "rain" && $split[2] == "wind" && $split[3] == "clouds" && $split[4] == "fog"){	
	echo '<img src="images/rain.jpg?image=.\img\default.jpg&height=200&width=640" />';
	echo '<img src="images/wind.jpg?image=.\img\default.jpg&height=200&width=640" />';
	echo '<img src="images/clouds.jpg?image=.\img\default.jpg&height=200&width=640" />';
	echo '<img src="images/fog.jpg?image=.\img\default.jpg&height=200&width=640" />';
	}
else{
	echo '<img src="images/open.jpg?image=.\img\default.jpg&height=200&width=640" />';
	}

?>


</div>
</body>
</html>
Last edited by Benjamin on Tue Jan 18, 2011 10:08 pm, edited 2 times in total.
Reason: Added [syntax=php] tags.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Help with Logic Operators

Post by Jonah Bron »

We need to see a data sample of your comma delimited list, and you need to thoroughly describe how the logic should work. Also, please edit your original post to surround your code with

Code: Select all

 [/ syntax] tags (use the PHP Code button).
Post Reply