Jump to content

im stuck with my code. IF this = that show image code.


Go to solution Solved by djzman,

Recommended Posts

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)

 

 

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.

Thanks Guru but its not loading the image.    I do have image1.png   image2.png and image3.png
www.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. 

@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);
    }
}
  • Solution

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.

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/home

Thank you again for all the help.  

Edited by djzman

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

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

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.

 

 

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.