|
|
|
date: Wed, 07 May 2008 07:56:16 +0100,
group: uk.net.web.authoring
back
Finding the current file name in php
I'm sure there are better menu systems around (one day perhaps I will
look into finding another one), but
http://www.g8wrb.org/
has 18 menus on the top. My plan was to ensure that if you select a
menu, you were presented with 17 others to chose, and not the page you
are actually at. (The 'Uploads', "YagiUda" and 'Home' are hadled
differently and I'm not worried about them.)
Click on "YC156" there, and you will see YC156 is not in the menu any
more. Click on Y799 and the same happens. Now click on "Mathematica" and
Mathematica is presented in the menu, which *not* what I want.
The code for the navigation menu is at
http://www.g8wrb.org/navigation.phps
Basically the menu is dynamically generated. Each item on the list is
simply echoded, except for the line that contains the menu item I am
currently on.
e.g.
The difference between those that work, and those that do not is that
those in the root directory (y799.php, yc156.php) all work, but those in
subdirectories (mathematica/index.php, triodes/index.php) do not.
So this code
if( strpos($_SERVER['PHP_SELF'],"y799.php")==false )
echo "<li><a
href=\"http://www.g8wrb.org/y799.php\">Y799</a></li>\n";
works.
But this code
if( strpos($_SERVER['PHP_SELF'],"mathematica/index.php")==false )
echo "<li><a
href=\"http://www.g8wrb.org/mathematica/\">Mathematica</a></li>\n";
does not.
I've linked a few .phps to the .php files, so
http://www.g8wrb.org/mathematica/index.phps
http://www.g8wrb.org/index.phps
http://www.g8wrb.org/navigation.phps
http://www.g8wrb.org/y799.phps
You can see most simple have a line to include
http://www.g8wrb.org/navigation.phps
If you have any ideas how I can sort this out, please let me know.
date: Wed, 07 May 2008 07:56:16 +0100
author: Dave
|
Re: Finding the current file name in php
<uk.net.web.authoring>
<Dave>
<Wed, 07 May 2008 07:56:16 +0100>
> I'm sure there are better menu systems around (one day perhaps I will
> look into finding another one), but
>
<?php $temp=basename($_SERVER['PHP_SELF']); ?>
Its too early in the morning and i didnt click on any of the links - but
above might be the sort of thing your looking for rather than strpos() .
--
www.krustov.co.uk
date: Wed, 7 May 2008 09:44:34 +0100
author: Krustov
|
Re: Finding the current file name in php
In message , Dave writes
>The difference between those that work, and those that do not is that
>those in the root directory (y799.php, yc156.php) all work, but those in
>subdirectories (mathematica/index.php, triodes/index.php) do not.
>
>So this code
>
> if( strpos($_SERVER['PHP_SELF'],"y799.php")==false )
> echo "<li><a href=\"http://www.g8wrb.org/y799.php\">Y799</a></l
>i>\n";
>
>works.
>
>But this code
>
> if( strpos($_SERVER['PHP_SELF'],"mathematica/index.php")==false )
> echo "<li><a href=\"http://www.g8wrb.org/mathematica/\">Mathem
>atica</a></li>\n";
>
>does not.
A couple of thoughts:
echo the value returned by $_SERVER['PHP_SELF'] to make sure it
is what you expect
set a variable in each file that includes the navigation file,
before the include, and use that for the comparison
Finally the manual for strpos has this warning:
This function may return Boolean FALSE, but may also return a
non-Boolean value which evaluates to FALSE, such as 0 or "".
Please read the section on Booleans for more information. Use
the === operator for testing the return value of this function.
--
Dominic Sexton
date: Wed, 7 May 2008 10:03:37 +0100
author: Dominic Sexton {da-sep03}@dscs.demon.co.uk
|
Re: Finding the current file name in php
Dominic Sexton wrote:
> In message , Dave writes
>> The difference between those that work, and those that do not is that
>> those in the root directory (y799.php, yc156.php) all work, but those in
>> subdirectories (mathematica/index.php, triodes/index.php) do not.
>>
>> So this code
>>
>> if( strpos($_SERVER['PHP_SELF'],"y799.php")==false )
>> echo "<li><a href=\"http://www.g8wrb.org/y799.php\">Y799</a></l
>> i>\n";
>>
>> works.
>>
>> But this code
>>
>> if( strpos($_SERVER['PHP_SELF'],"mathematica/index.php")==false )
>> echo "<li><a href=\"http://www.g8wrb.org/mathematica/\">Mathem
>> atica</a></li>\n";
>>
>> does not.
>
> A couple of thoughts:
>
> echo the value returned by $_SERVER['PHP_SELF'] to make sure it
> is what you expect
I had done that, and it is. In fact, if you look at
http://www.g8wrb.org/triodes/
http://www.g8wrb.org/triodes/index.phps
you will see it in the top right.
>
> set a variable in each file that includes the navigation file,
> before the include, and use that for the comparison
Sorry, you have lost me there.
> Finally the manual for strpos has this warning:
>
> This function may return Boolean FALSE, but may also return a
> non-Boolean value which evaluates to FALSE, such as 0 or "".
> Please read the section on Booleans for more information. Use
> the === operator for testing the return value of this function.
I'll check that out.
date: Wed, 07 May 2008 10:48:19 +0100
author: Dave
|
Re: Finding the current file name in php
Message-ID: from Dominic Sexton
contained the following:
>>But this code
>>
>> if( strpos($_SERVER['PHP_SELF'],"mathematica/index.php")==false )
>> echo "<li><a href=\"http://www.g8wrb.org/mathematica/\">Mathem
>>atica</a></li>\n";
>>
>>does not.
Actually, that's not what you have in your code. Note the forward slash
in front of mathematica.
if( strpos($_SERVER['PHP_SELF'],"/mathematica/index.php")==false )
echo "<li><a
href=\"http://www.g8wrb.org/mathematica/\">Mathematica</a></li>\n";
As Dominic pointed out this evaluates to a non-Boolean value which
evaluates to FALSE, in this case, 0
=== should sort it out
However, I find the code to be a bit long winded.
Why not just do a simple comparison?
if( $_SERVER['PHP_SELF']!="/mathematica/index.php")
echo "<li><a
href=\"http://www.g8wrb.org/mathematica/\">Mathematica</a></li>\n";
Even better, I'd set up an array of links (which could possibly be
dynamically generated eventually)
$links=array("Mathematica"="/mathematica/index.php","Triodes"=>"/triodes/index.php","Other
file"=>"/otherfile.php", ...add more links as required);
Then:
foreach($links as $key=$value){
if( $_SERVER['PHP_SELF']!=$value)
echo "<li><a href=\"$value\">$key</a></li>\n";
}
}
--
Regards,
Geoff Berrow
date: Wed, 07 May 2008 11:49:06 +0100
author: Geoff Berrow
|
Re: Finding the current file name in php
Geoff Berrow wrote:
> Message-ID: from Dominic Sexton
> contained the following:
>
>>> But this code
>>>
>>> if( strpos($_SERVER['PHP_SELF'],"mathematica/index.php")==false )
>>> echo "<li><a href=\"http://www.g8wrb.org/mathematica/\">Mathem
>>> atica</a></li>\n";
>>>
>>> does not.
>
> Actually, that's not what you have in your code. Note the forward slash
> in front of mathematica.
>
> if( strpos($_SERVER['PHP_SELF'],"/mathematica/index.php")==false )
> echo "<li><a
> href=\"http://www.g8wrb.org/mathematica/\">Mathematica</a></li>\n";
>
> As Dominic pointed out this evaluates to a non-Boolean value which
> evaluates to FALSE, in this case, 0
>
> === should sort it out
>
> However, I find the code to be a bit long winded.
>
> Why not just do a simple comparison?
> if( $_SERVER['PHP_SELF']!="/mathematica/index.php")
> echo "<li><a
> href=\"http://www.g8wrb.org/mathematica/\">Mathematica</a></li>\n";
>
> Even better, I'd set up an array of links (which could possibly be
> dynamically generated eventually)
>
> $links=array("Mathematica"="/mathematica/index.php","Triodes"=>"/triodes/index.php","Other
> file"=>"/otherfile.php", ...add more links as required);
>
> Then:
>
> foreach($links as $key=$value){
> if( $_SERVER['PHP_SELF']!=$value)
> echo "<li><a href=\"$value\">$key</a></li>\n";
> }
> }
>
Thanks a lot for that.
I implemented something based on what you suggested. I decided to move
ALL .php files (with the exception of the menu itself and the main home
page) to subdirectories.
i.e what was previously
http://www.g8wrb.org/y799.php
is now
http://www.g8wrb.org/y799/index.php
This means people will not have .php on the end of any links. Hence I
need to append index.php to my links before comparing with
$_SERVER['PHP_SELF']. But the following seems to be ok.
$links=array(
"Mathematica"=>"/mathematica/",
"Triodes"=>"/triodes/",
"Tetrodes"=>"/tetrodes/",
"Pentodes"=>"/pentodes/",
"Bases"=>"/bases/",
"Goodies"=>"/useful-stuff/",
"Application notes"=>"/application-notes/",
"Browse"=>"/data/",
"Thanks to"=>"/thanks/",
"Goodies"=>"/useful-stuff/",
"Solaris laptop"=>"/useful-stuff/Sun/laptop/",
"Y799"=>"/y799/",
"YC156"=>"/yc156/",
"New data"=>"/recently-added/",
"Yagi Uda"=>"/yagi/",
"Help"=>"/help/",
"Links"=>"/links/"
);
foreach($links as $key=>$value){
// compare the actual file name with the data above, after
appending index.php"
$fullfile=$value."index.php";
$f=$_SERVER['PHP_SELF'] ;
// echo "$f $fullfile<br><br>";
if( $_SERVER['PHP_SELF'] !=$fullfile)
echo "<li><a href=\"$value\">$key</a></li>\n";
}
I might look some time at creating some sub-menus. Even if my menus are
not the best in the world, I'd rather something I understand, than some
black-magic which works.
date: Wed, 07 May 2008 16:57:28 +0100
author: Dave
|
Re: Finding the current file name in php
Message-ID: from Dave contained the following:
>I might look some time at creating some sub-menus. Even if my menus are
>not the best in the world, I'd rather something I understand, than some
>black-magic which works.
It's an approach I adopt. If you can understand it, you can tweak it to
suit your needs.
Next step would be to make the menu array self generating, which I'll
leave as an exercise :-)
--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
date: Wed, 07 May 2008 17:12:57 +0100
author: Geoff Berrow
|
|
|