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 :
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.
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.