Page 1 of 1

if statement not typed good or PHP bug?

Posted: Sat Sep 01, 2007 7:44 am
by philc
hi. I'm using a small function I built. It always worked. Now it does not, I tried to test if all the variable work and they do. but the if statement does not seem to work. here is the code:-

Code: Select all

function look_pic01() {
	
	$filename = "../../beginpagina.html";
	$fp = fopen($filename, "r") or die("Could'nt open $filename");
	
	$g_pic01 = "";
	
	$i = 0;
               // get from line 20 picture name and store it in $g_pic01
	while (!feof($fp)) {
		$i++;
		$line = fgets($fp, 1024);
		if ($i === 20) {$g_pic01 = $line;}
	} 
	
	$path = "../../pictures/"; 
	echo "<select name=\"pic01\">"; 
	echo "<option value=\"\"></option>";
		foreach(glob($path.'*.jpg') as $e) { 
			$check_pic01 = basename($e);
			$sel_pic01 = "";
			if ($g_pic01 === $check_pic01) {$sel_pic01 = "selected=\"selected\"";}
			echo "<option value=\"".$check_pic01."\" ".$sel_pic01.">".$check_pic01." ".$sel_pic01."</option>\n"; 
		} 
	
	echo "</select>";
}
what the function does is list all the pictures into a drop down menu. what what it does not work is the variable $sel_pic01 which stores the value selected="selected" if the picture name from html file and picture name from foreach loop matches in the if statement. Can anyone please help me? i really tried my best. thanks in advance

Posted: Sat Sep 01, 2007 7:50 am
by volka
please try

Code: Select all

<?php
function look_pic01() {

  $filename = "../../beginpagina.html";
  $fp = fopen($filename, "r") or die("Could'nt open $filename");

  $g_pic01 = "";

  $i = 0;
  // get from line 20 picture name and store it in $g_pic01
  while (!feof($fp)) {
    $i++;
    $line = fgets($fp, 1024);
    if ($i === 20) {$g_pic01 = $line;}
  }

  $path = "../../pictures/";
  // echo "<select name=\"pic01\">";
  // echo "<option value=\"\"></option>";
  foreach(glob($path.'*.jpg') as $e) {
    $check_pic01 = basename($e);
    $sel_pic01 = "";
    
    if ($g_pic01 === $check_pic01) {$sel_pic01 = "selected=\"selected\"";}
    
    echo "'$g_pic01' === '$check_pic01' ", ($g_pic01 === $check_pic01) ? 'true':'false', ' # ', $sel_pic01, "<br />\n";
    
    //echo "<option value=\"".$check_pic01."\" ".$sel_pic01.">".$check_pic01." ".$sel_pic01."</option>\n";
  }

  // echo "</select>";
}
and post the output.

Posted: Sat Sep 01, 2007 8:23 am
by philc
output was:

Left Picture:'others3.jpg ' === 'check_pic_a.jpg' false #
'others3.jpg ' === 'check_pic_b.jpg' false #
'others3.jpg ' === 'hover_bg.jpg' false #
'others3.jpg ' === 'others3.jpg' false #
'others3.jpg ' === 'others4.jpg' false #
'others3.jpg ' === 'title_bg.jpg' false #

I tried to test the variables before but they don't seem to be the problem.

Posted: Sat Sep 01, 2007 9:05 am
by volka
Take a close look at the left side of the === in the output. Do you see the space between others3.jpg and the single quote?
Try again with

Code: Select all

if ($i === 20) {$g_pic01 = trim($line);}

Posted: Sun Sep 02, 2007 1:34 am
by philc
it still does not work...does anyone has more ideas what could it be?

Posted: Sun Sep 02, 2007 1:45 am
by califdon
philc wrote:it still does not work...does anyone has more ideas what could it be?
If you fixed the space problem that volka pointed out, please run his code again. I would be greatly surprised if it showed:

Code: Select all

'others3.jpg' === 'others3.jpg' false #

Posted: Sun Sep 02, 2007 1:51 am
by s.dot
The space is definately your problem.

Code: Select all

if (trim($g_pic01) === $check_pic01) {$sel_pic01 = "selected=\"selected\"";}

Posted: Sun Sep 02, 2007 5:52 am
by volka
califdon wrote:I would be greatly surprised if it showed:

Code: Select all

'others3.jpg' === 'others3.jpg' false #
It would be a strange (and hopefully unique) version of php ;)

Posted: Sun Sep 02, 2007 6:02 am
by philc
yepss it was that space.......i did a mistake myself that's why it did not work :P.

thanks guys for your help especially volka

now i can go back to work :D