djzman Posted March 20, 2014 Share Posted March 20, 2014 Im stuck. would someone please help me. I can get it to echo the number but not a image. ---------------------- <?php if (file_exists('number.txt')) { $fil = fopen('number.txt', r); $dat = fread($fil, filesize('number.txt')); if ($dat = 1) echo '<img src="image1.png"'); if ($dat = 2) echo '<img src="image2.png"'); if ($dat = 3) echo '<img src="image3.png"'); fclose($fil); } ?> -------------------------- the number.text is in the same dir. the image's are in the same dir and the php code is in the same dir. and the number.txt i set to 777 (just incase it needed to be) thanks for any help. (I hope i wrote this in the right form if not sorry) Quote Link to comment https://forums.phpfreaks.com/topic/287117-im-stuck-with-my-code-if-this-that-show-image-code/ Share on other sites More sharing options...
Ch0cu3r Posted March 20, 2014 Share Posted March 20, 2014 You're using the wrong operator for comparing values. You use = (assignment operator) to a assign a value to variable You use == (comparison operator) to compare a value Quote Link to comment https://forums.phpfreaks.com/topic/287117-im-stuck-with-my-code-if-this-that-show-image-code/#findComment-1473279 Share on other sites More sharing options...
cyberRobot Posted March 20, 2014 Share Posted March 20, 2014 Also, you have some extra parenthesis after the echo statements. For what it's worth, you could simplify the code by using file_get_contents(). You also don't need the separate if tests for $dat. You could try something like this: <?php if (file_exists('number.txt')) { $fil = fopen('number.txt', r); $dat = fread($fil, filesize('number.txt')); echo '<img src="image' . $dat . '.png"'; fclose($fil); } ?> Of course, you should check that the image file exists before trying to display it. Quote Link to comment https://forums.phpfreaks.com/topic/287117-im-stuck-with-my-code-if-this-that-show-image-code/#findComment-1473293 Share on other sites More sharing options...
djzman Posted March 20, 2014 Author Share Posted March 20, 2014 Thanks Guru but its not loading the image. I do have image1.png image2.png and image3.pngwww.wisconsinstormchasers.com/testing123/image1.png www.wisconsinstormchasers.com/testing123/image2.png www.wisconsinstormchasers.com/testing123/image3.png and number.txt is set to 1 www.wisconsinstormchasers.com/testing123/number.txt and the index.php has your code in it. I did make arue to make number.txt Read Write and executable (incase it needed to be) so something is still wrong that its not loading. Quote Link to comment https://forums.phpfreaks.com/topic/287117-im-stuck-with-my-code-if-this-that-show-image-code/#findComment-1473378 Share on other sites More sharing options...
Psycho Posted March 20, 2014 Share Posted March 20, 2014 @CyberRobot, I would suggest not using the name directly in the file as it opens up some security risks. I'd at least run intval() on it to make sure it is safe to use in a file name. @djzman, add some code to VERIFY what is happening $file = 'number.txt'; if (!file_exists($file)) { echo "Could not find {$file}<br>\n"; } else { $fil = fopen('number.txt', r); if(!$fil) { echo "Could not open {$file}<br>\n"; } else { $dat = fread($fil, filesize('number.txt')); if(!$dat) { echo "Could not read from {$file}<br>\n"; } else { echo "The extracted value from {$file} was $dat<br>\n"; echo "<img src='image{$dat}.png' />"; } fclose($fil); } } Quote Link to comment https://forums.phpfreaks.com/topic/287117-im-stuck-with-my-code-if-this-that-show-image-code/#findComment-1473381 Share on other sites More sharing options...
djzman Posted March 20, 2014 Author Share Posted March 20, 2014 @CyberRobotThis is working see for your self, www.wisconsinstormchasers.com/testing123 Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/287117-im-stuck-with-my-code-if-this-that-show-image-code/#findComment-1473390 Share on other sites More sharing options...
Solution djzman Posted March 21, 2014 Author Solution Share Posted March 21, 2014 Also, you have some extra parenthesis after the echo statements. For what it's worth, you could simplify the code by using file_get_contents(). You also don't need the separate if tests for $dat. You could try something like this: <?php if (file_exists('number.txt')) { $fil = fopen('number.txt', r); $dat = fread($fil, filesize('number.txt')); echo '<img src="image' . $dat . '.png"'; fclose($fil); } ?> Of course, you should check that the image file exists before trying to display it. I got this one now to work thanks to CyberRobot I changed this: echo '<img src="image' . $dat . '.png"'; to this: echo "<img src='image{$dat}.png' />"; and now that code is working. So thanks everyone. Quote Link to comment https://forums.phpfreaks.com/topic/287117-im-stuck-with-my-code-if-this-that-show-image-code/#findComment-1473414 Share on other sites More sharing options...
djzman Posted March 21, 2014 Author Share Posted March 21, 2014 (edited) Thank you everyone for helping me get this code right. I know sometimes when you help people you never get to see what they are doing.I run Wisconsin Storm Chasers and we have a alert (red yellow green) flashing light on our main website. and i cant be around the computer all the time to log in to change it from one color to another when weather changes. so now the members of the club who have the link can do it. so it gets changed faster to warn the public. here is a test page i was working on, you can see it working now. http://wisconsinstormchasers.com/testing123 and if you go to our main website you can see where the light is on the page http://wisconsinstormchasers.com/homeThank you again for all the help. Edited March 21, 2014 by djzman Quote Link to comment https://forums.phpfreaks.com/topic/287117-im-stuck-with-my-code-if-this-that-show-image-code/#findComment-1473428 Share on other sites More sharing options...
cyberRobot Posted March 21, 2014 Share Posted March 21, 2014 As Psycho suggested, you'll want to validate the number that comes from the text file. You could use something like the following: <?php $validValues = array(1,2,3); if (file_exists('number.txt')) { $fil = fopen('number.txt', r); $dat = fread($fil, filesize('number.txt')); if(in_array($dat, $validValues)) { echo "<img src='image{$dat}.png' />"; } fclose($fil); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/287117-im-stuck-with-my-code-if-this-that-show-image-code/#findComment-1473446 Share on other sites More sharing options...
djzman Posted March 22, 2014 Author Share Posted March 22, 2014 how do i make this so i can click on the alertbox gif Thanks all. <?php if (file_exists('number.txt')) { $fil = fopen('number.txt', r); $dat = fread($fil, filesize('number.txt')); echo "<img src='alertbox{$dat}.gif'>"; fclose($fil); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/287117-im-stuck-with-my-code-if-this-that-show-image-code/#findComment-1473551 Share on other sites More sharing options...
cyberRobot Posted March 23, 2014 Share Posted March 23, 2014 Have you tried using an anchor tag? https://www.google.com/search?q=html+anchor+tag Quote Link to comment https://forums.phpfreaks.com/topic/287117-im-stuck-with-my-code-if-this-that-show-image-code/#findComment-1473639 Share on other sites More sharing options...
djzman Posted March 24, 2014 Author Share Posted March 24, 2014 Have you tried using an anchor tag? https://www.google.com/search?q=html+anchor+tag Yes sir i did. Oh and I did fig it out last night after looking over the image code. Since i seen they used ' instead of " i tryed echo "<a href='http://alerts.weather.gov/cap/wi.php?x=1' target='_large'><img src='image{$dat}.png'></a>"; And that worked. what i was doing wrong before im use to in php and html using the " instead of the ' and thats what did me wrong. But I do thank you for those links. W3 i use that website a lot, but when i get stuck i come here. Thank again. Quote Link to comment https://forums.phpfreaks.com/topic/287117-im-stuck-with-my-code-if-this-that-show-image-code/#findComment-1473742 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.