vezinadj Posted December 31, 2010 Share Posted December 31, 2010 Please keep in mind, im a newbi at php as well as these forums, and I just started using it this week. I have some background in C programing and know a little VB. I am currently trying to make a php based webpage that has 5 different images on it. I want each image to change with respect to what is written in a text file within the server space. So to better explain this, I have a text file that is named "status.txt". status.txt contents: active inactive active active active I want the first image to change to a different image if I change the first line of the text file to be inactive instead of active and the 2nd image to change if I change the 2nd line (inactive) to active and so forth on all the other images. I do know that i need to open the file, then read the file with fopen and fread and set what the contents that i read from the file equal to some variable (still not 100% sure im using the functions right). but im not sure how to set up the code so the image will change with respect to what is read from the text aka what the variable is equal too. I thought it would be a simple if statement but it seems more complicated then that. and futhermore, im not sure where to place the open file, should i do this in the header? so every time website refreshes, is opens the file and reads the txt file and changes with respect to that? Any help would be great! Thank you in advance. Quote Link to comment Share on other sites More sharing options...
vezinadj Posted December 31, 2010 Author Share Posted December 31, 2010 This is the code im currently trying to use in place of the image within the table I made. <?php $textfile = "machinestatus.txt"; $openfile = fopen($textfile, 'r'); $status = fread($openfile, 6); if ($status = active){echo "images/caronlayout_active.gif";} elseif ($status = inactive){echo "images/caronlayout_inactive.gif";} elseif ($status = alarm){echo "images/caronlayout_alarm.gif";} else {echo "images/caronlayout_offline.gif";} fclose($openfile); ?> Quote Link to comment Share on other sites More sharing options...
litebearer Posted December 31, 2010 Share Posted December 31, 2010 just a rough idea... presumption images are named active - image1.jpg - inactive image2.jpg active - image3.jpg - inactive image4.jpg etc etc the following should put the names of the appropriate images in an array. $textfile = "machinestatus.txt"; $image_status = file($textfile); $i=0; $w=1; while($i<5) { if($image_status[$i]=="active") { $image_array[$i] = "image" . $w . ".jpg"; }else( $image_array[$i] = "image" . ($w+1) . ".jpg"; } $w = $w + 2; $i ++; } /* to display the image appropriate for line three in your status file */ ?> <IMG src="<?PHP echo $image_array[2]; ?>"> <?PHP Quote Link to comment Share on other sites More sharing options...
vezinadj Posted December 31, 2010 Author Share Posted December 31, 2010 So im assuming the php portion of the code goes before all of my html code in my source code, and the IMG source at the end of your code snippet is what i put in my table calling the php function? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.