if statement not typed good or PHP bug?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
philc
Forum Newbie
Posts: 9
Joined: Sat Jul 21, 2007 3:48 am

if statement not typed good or PHP bug?

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
philc
Forum Newbie
Posts: 9
Joined: Sat Jul 21, 2007 3:48 am

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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);}
philc
Forum Newbie
Posts: 9
Joined: Sat Jul 21, 2007 3:48 am

Post by philc »

it still does not work...does anyone has more ideas what could it be?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post 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 #
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

The space is definately your problem.

Code: Select all

if (trim($g_pic01) === $check_pic01) {$sel_pic01 = "selected=\"selected\"";}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 ;)
philc
Forum Newbie
Posts: 9
Joined: Sat Jul 21, 2007 3:48 am

Post 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
Post Reply