Page 1 of 1

If elseif else error and help

Posted: Sat Jan 01, 2011 3:15 pm
by vezinadj
Heres what I am trying to accomplish.

I am currently trying to read a line in a text document and compare it to a set variable to change an image on my page.
The text document currently just says either "on" or "off" or "offline"

This is the code I am trying to implement:
<?php

$myFile = "status.txt";

$fh = fopen($myFile, 'r');

$theData = fread($fh, 8);

fclose($fh);

$onData = "on";

$offData = "off";
$offlineData = "offline";


if($theData==$onData){

echo '<img src="image1.gif" width="61" height="60" alt="">';
}

elseif($theData==$offData){

echo '<img src="image2.gif" width="61" height="61" alt="">';
}

else($theData==$offlineData)
echo '<img src="image3.gif" width="61" height="61" alt="">';

?>

I am new to php programming so be easy on me. The first image works when I have the text document showing on (the on image works). The off image works as well when off is saved in the text document. When it gets to the offline image, I put offline in my text document and nothing shows up, its just a blank image. Anyone have any suggestions?

Re: If elseif else error and help

Posted: Sat Jan 01, 2011 3:52 pm
by Neilos
You can't do this;

Code: Select all

else($theData==$offlineData)
echo '<img src="image3.gif" width="61" height="61" alt="">';	
There is all kinds of wrong going on there lol. Try;

Code: Select all

elseif($theData==$offlineData) {
echo '<img src="image3.gif" width="61" height="61" alt="">';
}
Or;

Code: Select all

else {
echo '<img src="image3.gif" width="61" height="61" alt="">';
}

Re: If elseif else error and help

Posted: Sat Jan 01, 2011 4:05 pm
by vezinadj
It seems if i change the operand, == in the second elseif statement, the 2nd image shows up. So im looking into the fact that the operands must be wrong, but if theData isnt equal what is in the text document, i should still be seeing the offline image, which isnt happening.

Is it possible to use all if statements? or is this the best way to approach this?

Re: If elseif else error and help

Posted: Sat Jan 01, 2011 4:06 pm
by Neilos
I just thought that I didn't really explain why so I thought I'd give you the heads up.

The two things that were glaring at me from that code straight away were the fact that you have;

1) a condition on an else statement.

This is incorrect, if statements and elseif statements both have conditions but the else statement does not. Check http://www.htmlite.com/php015.php out.

2) no curly braces on the else statement.

As far as I am aware this is a no no.

When writing code it is always handy to check the resources, the two I use the most are http://php.net/ and
http://www.w3schools.com/php/. I find them really handy to check back on when I am stuck or trying something new.

Hope this helps :D

Ahhh 1 sec u replied before I added my new message I'll reply in a sec.

Re: If elseif else error and help

Posted: Sat Jan 01, 2011 4:23 pm
by Neilos
Yes it is possible to use all if statements if you wish There will always be more efficient ways to handle it though but I wouldn't be able to tell you the most efficient without testing different ways. If you have not very much for the CPU on the server to do then you can brute force you way to the end all you like. I always find that it is best to get something that works before worrying about the best way to implement it.

With that in mind, for now, I think it is safe to carry on the way you have started.

If you are now getting just the default action (ie the one in the else statement), whatever you do, then that means that the conditions in your if and elseif statements are never being met even when they should be. I think that this is now your problem, correct me if I am wrong.

Regarding the == operand I don't know what you are changing it to, could you please say?

I would first check that the if statements are working correctly and that they display the images correctly.

Try;

Code: Select all

<?php 

$user=1;

if ($user==1){
	echo '<img src="image1.gif" width="61" height="60" alt="">';
} elseif ($user==2){
	echo '<img src="image2.gif" width="61" height="61" alt="">';
} else {
	echo '<img src="image3.gif" width="61" height="61" alt="">';
}

?>
Then make $user equal to 2 and then 3 and see if the output is correct for what you would expect.

Let me know the results

Re: If elseif else error and help

Posted: Sat Jan 01, 2011 4:27 pm
by vezinadj
Thank you again for your reply.

This is what I have right now.

if($theData==$on){

echo '<img src="on.gif" width="61" height="60" alt="">';
}

elseif($theData=$off){

echo '<img src="off.gif" width="61" height="61" alt="">';
}

else{
echo '<img src="offline.gif" width="61" height="61" alt="">';
}

For some reason, when I change the text document to "on", the on image shows up, which is good, and when I change it too "off" the off image shows up, which is also good. But with the else statement (correct me if im wrong), i should be able to put anything but on or off and the offline image should show up correct?
When typing anything into the text document, like "hello", the image does not change from its previous state. I feel im close to finishing this, but yet so far.

I appreciate the help once again and your prompt responses.

Re: If elseif else error and help

Posted: Sat Jan 01, 2011 4:36 pm
by Neilos
Could you please put the full source up as you seem to have changes a variable name and I don't know what is going on lol

I think you must be initialising the variables;

Code: Select all

$on = on;

$off = off;
Instead of;

Code: Select all

$onData = on;

$offData = off;
Is this correct? I just want a sanity check quickly lol. Because now I'm worried that your not initialising the variables.

Also did you try the test I gave you. That will narrow it down to either the if statements or where you read the file.

You could also do this to check that you are reading the file correctly;

Code: Select all

<?php

$myFile = "status.txt";

$fh = fopen($myFile, 'r');

$theData = fread($fh, 8);

fclose($fh);

echo $theData;

?>
And see if it echos what you expect.

Let me know all the results.

Re: If elseif else error and help

Posted: Sat Jan 01, 2011 4:42 pm
by vezinadj
the code you linked worked as I changed the variable user. And I have implemented my code into it one by one.
The steps I took:

1. Implementing the read file portion, and echo'ing the data to make sure im getting what i wrote in the text document in the desired variable(worked fine)
2. Implemented theData variable into the if statements you gave me, and again, when on is typed into the text document, it worked fine. But when i changed the text to off, it went to the offline picture. BUT if I change the operand of the ifelse statement, to a single =, then the off one works, and the offline doesnt work. Maybe theres some issue with trying to compare characters from the document to an unknown variable? as in,
on = on (works)
off = off (works)
anything else = offline <---- doesnt work?

This is what im working with right now.

<?php
$myFile = "text.txt";

$fh = fopen($myFile, 'r');

$theData = fread($fh, 6);
fclose($fh);


$onData = "on";

$offData = "off";

if ($theData==$onData){
echo '<img src="on.gif" width="61" height="60" alt="">';
} elseif ($theData=$offData){
echo '<img src="off.gif" width="61" height="61" alt="">';
} else {
echo '<img src="offline.gif" width="61" height="61" alt="">';
}
?>

Re: If elseif else error and help

Posted: Sat Jan 01, 2011 4:53 pm
by Neilos
vezinadj wrote: But with the else statement (correct me if im wrong), i should be able to put anything but on or off and the offline image should show up correct?
You are not wrong.

Changing the == operator to a single = is incorrect, the two are completely different a single = means make equal to where as == means is it equal to. You need to be using the == in the if statement conditions.

Give me a sec I'm going to try it on my server to get a working version.

Re: If elseif else error and help

Posted: Sat Jan 01, 2011 5:12 pm
by Neilos
So this is what I am using and it works for me;

Code: Select all

<?php
$myFile = "text.txt";

$fh = fopen($myFile, 'r');

$theData = fread($fh, filesize($myFile));

fclose($fh);

$theData = trim($theData);

$onData = "on";

$offData = "off";

if ($theData == $onData){
	echo 'One';
} elseif ($theData == $offData){
	echo 'Two';
} else {
	echo 'Three';
}
?>
I thought that maybe some white space could be screwing you up so I added the trim function. This removes any spaces in the string and works for "on" and " on ". I also used the filesize function for completeness (However this does mean that if the text.txt is empty you will get an error). Try it out and let me know the results.

Re: If elseif else error and help

Posted: Sat Jan 01, 2011 5:27 pm
by vezinadj
This works great!
I will implement the images within the echo's and let you know how it goes. Thank you very much again, you have been a great deal of help!

Re: If elseif else error and help

Posted: Sat Jan 01, 2011 5:52 pm
by Neilos
It's my pleasure, it's great for me to get the practice in. Sorry if it was a little long winded though, I wanted to go with you through the problem solving process to help make you aware of what we were doing and why. It's great to break problems down in to parts to narrow down the problem area.

I think that the problem might have been the filesize defined in the fread, I didn't bother to check what the values meant, instead I just read the size and used that. Also the white space that I already mentioned could have been a problem but trim sorted that nicely.

Assuming you get no more problems with this... Happy coding!

:D