Jump to content

Help with dynamic image changing


vezinadj

Recommended Posts

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.

Link to comment
Share on other sites

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

            ?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.