Welcome Guest Search | Active Topics | Members | Log In | Register

Finding the current file name in php Options · View
Dave
Posted: Wednesday, May 07, 2008 8:56:16 AM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 07/05/2008 08:56:16
Date: 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

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.

Krustov
Posted: Wednesday, May 07, 2008 10:44:34 AM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 07/05/2008 10:44:34
Date: Wed, 7 May 2008 09:44:34 +0100

<uk.net.web.authoring>
<Dave>
<Wed, 07 May 2008 07:56:16 +0100>
<48215290@212.67.96.135>

> 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
Dominic Sexton
Posted: Wednesday, May 07, 2008 11:03:37 AM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 07/05/2008 11:03:37
Date: Wed, 7 May 2008 10:03:37 +0100

In message <48215290@212.67.96.135>, Dave <foo@coo.com> 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
Dave
Posted: Wednesday, May 07, 2008 11:48:19 AM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 07/05/2008 11:48:19
Date: Wed, 07 May 2008 10:48:19 +0100

Dominic Sexton wrote:
> In message <48215290@212.67.96.135>, Dave <foo@coo.com> 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.
Geoff Berrow
Posted: Wednesday, May 07, 2008 12:49:06 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 07/05/2008 12:49:06
Date: Wed, 07 May 2008 11:49:06 +0100

Message-ID: <82XXD1CpBXIIFwO1@nospam.demon.co.uk> 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
Dave
Posted: Wednesday, May 07, 2008 5:57:28 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 07/05/2008 17:57:28
Date: Wed, 07 May 2008 16:57:28 +0100

Geoff Berrow wrote:
> Message-ID: <82XXD1CpBXIIFwO1@nospam.demon.co.uk> 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.
Geoff Berrow
Posted: Wednesday, May 07, 2008 6:12:57 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 07/05/2008 18:12:57
Date: Wed, 07 May 2008 17:12:57 +0100

Message-ID: <4821d168@212.67.96.135> 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
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFPro Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.1 (NET v2.0) - 9/10/2007
Copyright © 2003-2006 Yet Another Forum.net. All rights reserved.
This page was generated in 0.089 seconds.