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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

This stupid IF issue is driving me nuts 1

Status
Not open for further replies.

pmonett

Programmer
Sep 5, 2002
2,632
FR
I am trying to create a dynamic menu for a website project.

To do so, I have a configuration file (pagelist.dat) that lists the menu entry and the corresponding file name under the form :

menuname-filename

I have created a function that reads the data file and populates two array variables with the menu option ($options) and the corresponding file name ($files).
This code works.

The next step is to display the menu, highlighting the menu option that corresponds to the page that is currently being displayed.

In order to do that, I catch the current page name from the server at the beginning of the script ($pagename).
Then, when iterating through the $files array, I check to see if, per chance, I'm not on the same one as the page that is currently loaded.
The idea is that, if yes, I use the CSS defined for the selected item, else I use the CSS for the unselected item.

Simple, in theory, but my menu never highlights the page I'm on !

Here is the code :
Code:
<?php
	$options[0]="";
	$files[0]="";
	$arraynum=0;
	$selected=0;
	$thisfile=$_SERVER["SCRIPT_NAME"];
	$tab=Explode('/',$thisfile);
	$pagename=$tab[count($tab)-1];
	function getoptions(){
	global $options, $files, $arraynum;
		$input="";
		$ficlist=file("pagelist.dat");
		foreach($ficlist as $input){
			$tab=explode("-",$input);
			$options[$arraynum]=$tab[0];
			$files[$arraynum]=$tab[1];
			$arraynum++;
		}
	}

	getoptions();
	
	echo "<table>";
	echo "<tr><td colspan='2' class='large_section'><br/><br/></td></tr>";
	echo "<tr><td class='menu_section'>";
	echo "<ul id='menu'>";
	$iter=0;
	for($iter=0;$iter<$arraynum;$iter++){
		echo "$pagename=$files[$iter]<br/>";
		if($files[$iter]==$pagename){
			echo "<li class='selected'><a href='".$files[$iter]."' >S".$options[$iter]."</a></li>";
		}else{
			echo "<li class='unselected'><a href='".$files[$iter]."' >".$options[$iter]."</a></li>";
		}
	}
	echo "</ul></td><td><img src='limo.jpg'></td>";
?>

I can add [blue]echo[/blue] commands all over the place, I only confirm that yes, the script knows the proper value of $pagename and the proper value of $files[$iter], but somehow when confronted in the [blue]if($files[$iter]==$pagename)[/blue], it totally forgets their value and moves for the [blue]else[/blue] part !

This is really getting under my skin, as it is a stupid thing that really should not hold me up at all.

Any ideas ? What have I done wrong ?

Pascal.

I've got nothing to hide, and I'd very much like to keep that away from prying eyes.
 
Perhaps there is an invisible extra character or some other difference between the names in your array, and what you are producing from $_SERVER['SCRIPT_NAME']

Can you post the output from this line:

echo "$pagename=$files[$iter]<br/>";

We may be able to tell you why its different.





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Spot on !

And I was being an idiot. Should've known that the file input sequence would keep the /r in the file name.

So now I need to prune /r from the end of my file names.

Which a [blue]Trim[/blue] will do nicely.

Well, thanks for the wake-up !

Pascal.

I've got nothing to hide, and I'd very much like to keep that away from prying eyes.
 
Glad I could help.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top