Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IF...Else based on file value 1

Status
Not open for further replies.

endoflux

Technical User
Aug 6, 2001
227
US
Okay, I'm a hack at this-- please tell me whats wrong in this bit:

I've got lvl.txt with a single character in it. If the character = 1, I want the graphic to show up; if not, I want a blank space. Currently, the character = 5, but my graphic is still showing up...

<?php
$getcurlvl = fopen("lvl.txt", "r");
$curlvl = fgets($getcurlvl, 1);
fclose($getcurlvl);
?>

<?php
if ($curlvl, "1") {
?>
<IMG ALT="^" SRC="images/arrow.gif">
<? } else { echo ' ';} ?>
 
What kind of if sentence syntax are you using? What is it supposed to mean? Have you tried simply:
Code:
if ($curlvl == "1")
{
  ...
}
We can't advice much more without actually knowing what $curlvl holds. Did you check the value that it has stored?
 
I'm not sure what you mean by sentence syntax, but $curlvl = fgets($getcurlvl, 1); was supposed to read:

"variable named curlvl equals the value in lvl.txt, 1 character long" (curlvl is a variable being created at that moment)

and "if curlvl = "1", display arrow.gif, else print a blank space"

I tried changing the syntax to if ($curlvl == "1"), but my image is still being displayed despite the value being "5".

I checked the file, and the value is "5"...and the "5" is on the first space of the first line of the file...and I know the file system is working, because I have no trouble changing that value from the form I created for it...ideas?


 
try
Code:
if ($curlvl === "1"){     //three equals signs

read the manual on equivalency operators. === means identical as to type and content.
 
No such luck.

I think the problem is somewhere in this stretch of code, because putting an "echo $curlvl;" later int he file is not printing my value either; I think it's not storing the value in $curlvl to begin with. (Once again, lvl.txt is a text file with only a single character 5 in it, in the first space, on the first line)

$getcurlvl = fopen("lvl.txt", "r");
$curlvl = fgets($getcurlvl, 1);
fclose($getcurlvl);
 
i think you are right. change the 1 to a 2 in the second line. the second parameter of fgets causes the function to read to length MINUS one bytes.
 
...and it works! That was it; just needed to set the fgets length to 2 so it would pull 1; odd that it subtracts one...

Thanks a ton!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top