Methods of marking a menu with the current page.

M

Mike Barnard

On Wed, 12 Mar 2008 20:23:14 +0000 (UTC), BootNic

Hi.

I have been working through this. Google has been my friend along with
a post in comp.lang.php! It's neat and not too difficult to get my
head around as an absolute beginner. Thanks for taking the time to
work this out, even if it was only a minute or two.

Do you mind if I ask a question or two to clarify things?
<ul class="menu">

Would it be normal to have HTML tags inside a PHP script when they
aren't part of an echo statement or similar?
<?php
#menu array
$menu = array('/index.php' => 'Home',
'/law.php' => 'The Law',
'/diy.php' => 'How to DIY',
'/hire.php' => 'Hire Me',
'/links.php' => 'Links',
'/contact.php' => 'Contact');

OK, I have this. An array of 6 items called "menu", the first value
being the content of the array and the second it's key, [reference or
address].
#current page
$location = $_SERVER["SCRIPT_NAME"];

This gets the value "/test/index.php" on my new install of WAMP5. I
have the www folder in WAMP5 and a subfolder called test. In this is
index.php.
#build menu items
foreach ($menu as $key => $value)

Go through a for loop, looking in $menu and assigning the content of
the array to the variable $key (which increases each iteration). I
think. More google.
{
echo " <li>\n";

Print said:
#if current page write span

Write span?
if ($key === $location)

OK, slight confusion here. === means is the value AND the type a
match. So, on the first loop $key contains "/index.php", $location
contains "/test/index.php". They are different so it should fail.
{
echo " <span>$value</span>\n";
}

If it was a match it would add the above to the said:
#else write a

Write a? What?
else
{
echo " <a href=\"$key\">$value</a>\n";
}

Create the statement said:
echo " </li>\n";

Finish it off with said:

End the foreach loop.

As the value of $location has the /test/ bit in it, and none of the
entries in the array have this, how can it ever be true?
 
B

BootNic

On Wed, 12 Mar 2008 20:23:14 +0000 (UTC), BootNic
[snip]

Do you mind if I ask a question or two to clarify things?
<ul class="menu">

Would it be normal to have HTML tags inside a PHP script when they
aren't part of an echo statement or similar?

That element is not inside the php script and there is no need for it to
be. PHP script starts with <php? and ends with ?> .

This "menu.php" does not even need a php extension, the extension can be
just about anything you wish it to be. It could be named menu.inc or
menu.txt.

[snip]
Write span?

A line that starts with "#" within a php script is a comment, it's intended
to give a clue as to what is being done. Comments can be removed.
OK, slight confusion here. === means is the value AND the type a
match. So, on the first loop $key contains "/index.php", $location
contains "/test/index.php". They are different so it should fail.

That is correct.

[snip]
Write a? What?

Another comment.
Create the statement <a href="/index.php">Home</a>;

Just what the comment was meant to suggest.
End the foreach loop.

As the value of $location has the /test/ bit in it, and none of the
entries in the array have this, how can it ever be true?

It can not be true, and it should not be true. If the page is
/test/index.php and there is no link in the menu for /test/index.php then
there is no current link.

Put an include in a page that is in the menu if you wish to see it work, or
add a link for /test/index.php to the menu.
 
J

Jonathan N. Little

Mike said:
On Wed, 12 Mar 2008 20:23:14 +0000 (UTC), BootNic

Hi.

I have been working through this. Google has been my friend along with
a post in comp.lang.php! It's neat and not too difficult to get my
head around as an absolute beginner. Thanks for taking the time to
work this out, even if it was only a minute or two.

Do you mind if I ask a question or two to clarify things?


Would it be normal to have HTML tags inside a PHP script when they
aren't part of an echo statement or similar?

In a PHP page only the bits between the <?php ...?> are php code. all
else is just static html.

<?php
#menu array
$menu = array('/index.php' => 'Home',
'/law.php' => 'The Law',
'/diy.php' => 'How to DIY',
'/hire.php' => 'Hire Me',
'/links.php' => 'Links',
'/contact.php' => 'Contact');

OK, I have this. An array of 6 items called "menu", the first value
being the content of the array and the second it's key, [reference or
address].

No it is an associative array where the index is a non-numeric key, here
is the URL, and the value is the link text.

#current page
$location = $_SERVER["SCRIPT_NAME"];

This gets the value "/test/index.php" on my new install of WAMP5. I
have the www folder in WAMP5 and a subfolder called test. In this is
index.php.
#build menu items
foreach ($menu as $key => $value)

Go through a for loop, looking in $menu and assigning the content of
the array to the variable $key (which increases each iteration). I
think. More google.

instead of iterating through a numbered array index here going through
the $key, $value pairs...
Write span?

Yes, for styling purposes so that you can have consistent padding, for
live link you pad the A elements for the on-page it's a SPAN

ul.menu li a,
ul.menu li span {
display: block; padding: .25em; border: 1px solid #666;
}

style both the same, else only the live A elements with have your border
and padding to keep text from being smack against the left side of the
block. If you pad the LI then the background color will not extend full
width...


OK, slight confusion here. === means is the value AND the type a
match. So, on the first loop $key contains "/index.php", $location
contains "/test/index.php". They are different so it should fail.

Evaluation, means if $key is equivalent to $location both in value and type.
Write a? What?

He means write an 'A' element
End the foreach loop.

As the value of $location has the /test/ bit in it, and none of the
entries in the array have this, how can it ever be true?

No the value of $location with have the name of the current script (the
page) either "/index.php", "/law.php", "/diy.php", "/hire.php",
"/links.php" or "/contact.php" and it will match one of the "keys" in
your array, unless you make a page and not add it to the list! Then all
links on your menu will be live and none mark as "on page".

BTW I would have your home as "/" not "/index.php" so it will work for
the default "www.example.com/"
 
B

BootNic

[]
BTW I would have your home as "/" not "/index.php" so it will work for
the default "www.example.com/"

It the home url in the menu array is "/" $_SERVER["SCRIPT_NAME"] will not
match it, simply because there is only a path. No script name to match to.

If the home url in the menu array is "/index.php" $_SERVER["SCRIPT_NAME"]
will match both "/" and "/index.php". Script name will return "/index.php"
for "/" because that is the script name. Presuming that index.php is the
default.
 
J

Jonathan N. Little

BootNic said:
[]
BTW I would have your home as "/" not "/index.php" so it will work for
the default "www.example.com/"

It the home url in the menu array is "/" $_SERVER["SCRIPT_NAME"] will not
match it, simply because there is only a path. No script name to match to.

If the home url in the menu array is "/index.php" $_SERVER["SCRIPT_NAME"]
will match both "/" and "/index.php". Script name will return "/index.php"
for "/" because that is the script name. Presuming that index.php is the
default.

Duh! I'm still thinking via $_SERVER['REQUEST_URI']!
 
M

Mike Barnard

That element is not inside the php script and there is no need for it to
be. PHP script starts with <php? and ends with ?> .

OK, I meant within the .php file which is being included. But I see
how it hangs together now, thanks.
This "menu.php" does not even need a php extension, the extension can be
just about anything you wish it to be. It could be named menu.inc or
menu.txt.

So, as long as the host server has a compatible PHP program running,
then it will find any said:
A line that starts with "#" within a php script is a comment, it's intended
to give a clue as to what is being done. Comments can be removed.

My bad description. I knew it was a comment, but I wondered why a
span was being written. I'm still a newbie you see.
That is correct.

Hey, I worked it out! :) (I know, a two year old could...)
[snip]
Write a? What?

Another comment.

Yebut, it was the 'a' bit I misunderstood. A what? A piece of cheese,
a bolt from the blue? But I get it now said:
Just what the comment was meant to suggest.

Remember yesterday was the first time I'd really bothered to look at
PHP code. I'm trying to digest it.
It can not be true, and it should not be true. If the page is
/test/index.php and there is no link in the menu for /test/index.php then
there is no current link.

But why? See next paragraph.
Put an include in a page that is in the menu if you wish to see it work, or
add a link for /test/index.php to the menu.

I have done, and I have added extra echo statements to show me the
values as they change. First I print the value of $location before the
loop runs, then the values of $key and $value on each iteration of the
loop. The results are...


$location: /test/index.php


$key: /index.php
$value: Home

$key: /law.php
$value: The Law

$key: /diy.php
$value: How to DIY

$key: /hire.php
$value: Hire Me

$key: /links.php
$value: Links

$key: /contact.php
$value: Contact


So the bit I do not understand is... none of the values of $key can
ever match the value of $location because they do not have the first 5
characters of $location, "/test". So why are there two statements in
the loop at all? The first statement can never be true, as far as I
can see. As usual, it will be something obvious, but I can't see it.
Can you help me to understand please?

#if current page write span
if ($key === $location)
{
echo " <span>$value</span>\n";
}


IF
$key[/index.php] is the same as $location[/test/index.php]
then
print to screen " <span>$value[Home]</span>\n"

If it was true then the final string would be just text noting the
current page, not an anchor pointing to a page. Understood, but how
can it ever be true?

Again, thanks for wasting your time on me. :)

Mike.
 
M

Mike Barnard

In a PHP page only the bits between the <?php ...?> are php code. all
else is just static html.
So you can do <img src="<?php makePix(); ?>" alt"php generated image">

Understood. I was thinking more of the fact that there is html in the
file as opposed to being within the <?php tags. Sinking slowly in.
#menu array
$menu = array('/index.php' => 'Home',
'/law.php' => 'The Law',
'/diy.php' => 'How to DIY',
'/hire.php' => 'Hire Me',
'/links.php' => 'Links',
'/contact.php' => 'Contact');

OK, I have this. An array of 6 items called "menu", the first value
being the content of the array and the second it's key, [reference or
address].

No it is an associative array where the index is a non-numeric key, here
is the URL, and the value is the link text.
#current page
$location = $_SERVER["SCRIPT_NAME"];

This gets the value "/test/index.php" on my new install of WAMP5. I
have the www folder in WAMP5 and a subfolder called test. In this is
index.php.
#build menu items
foreach ($menu as $key => $value)

Go through a for loop, looking in $menu and assigning the content of
the array to the variable $key (which increases each iteration). I
think. More google.
instead of iterating through a numbered array index here going through
the $key, $value pairs...

But in the order they were written in he script.
Yes, for styling purposes so that you can have consistent padding, for
live link you pad the A elements for the on-page it's a SPAN

More sinking in to the grey cells. Remembering, thats something else.
ul.menu li a,
ul.menu li span {
display: block; padding: .25em; border: 1px solid #666;
}

style both the same, else only the live A elements with have your border
and padding to keep text from being smack against the left side of the
block. If you pad the LI then the background color will not extend full
width...

AHA again.
Evaluation, means if $key is equivalent to $location both in value and type.

I thought thats what I said? :/
He means write an 'A' element

Heh, see my reply to him.
No the value of $location with have the name of the current script (the
page) either "/index.php", "/law.php", "/diy.php", "/hire.php",
"/links.php" or "/contact.php" and it will match one of the "keys" in
your array, unless you make a page and not add it to the list! Then all
links on your menu will be live and none mark as "on page".

Again, seeing my answer to Bootnic, the actual values returned never
ever match in the first loop. I won't repeat it all here.
BTW I would have your home as "/" not "/index.php" so it will work for
the default "www.example.com/"

Experimentaton to follow. Thanks.
 
J

Jonathan N. Little

Mike said:
On Thu, 13 Mar 2008 15:36:51 -0400, "Jonathan N. Little"


Again, seeing my answer to Bootnic, the actual values returned never
ever match in the first loop. I won't repeat it all here.

If you want it to match other than what you have in your menu array then
you will have to add it!, Oh not I see what you are asking, the example
given is for pages in your document root, If you wish it for "text" case
then change your "keys"

$menu = array('/test/index.php' => 'Home',
'/test/law.php' => 'The Law',
'/test/diy.php' => 'How to DIY',
'/test/hire.php' => 'Hire Me',
'/test/links.php' => 'Links',
'/test/contact.php' => 'Contact');
 
A

Andy Dingley

The higher specificity of id over class selectors has caused me grief in
the past, so I don't use id at all any more unless I've got a real
reason to.

Remember the bumper sticker:
"Id attributes don't kill CSS, id attributes in selectors kill
CSS."

It's not adding the id attribute to the HTML that's the problem, it's
trying to use it. You can add the thing easily and safely, even when
it's a duplicate of a class name. I often do this when I'm coding HTML
that I expect to have JS applied to it later, even though I'd strongly
agree with your avoidance of it as a CSS selector.
 
B

BootNic

[snip]
So, as long as the host server has a compatible PHP program running,
then it will find any <?php ... ?> in any file? Cool.

For any file that the server process php for, not just any file. The
include file is not processed its self, but if the include file contains
php and the file it's being included in is processed for php then it gets
processed. Clear as mud, yes?

[snip]
It can not be true, and it should not be true. If the page is
/test/index.php and there is no link in the menu for /test/index.php
then there is no current link.

But why? See next paragraph.
[snip]

So the bit I do not understand is... none of the values of $key can
ever match the value of $location because they do not have the first 5
characters of $location, "/test". So why are there two statements in
the loop at all? The first statement can never be true, as far as I
can see. As usual, it will be something obvious, but I can't see it.
Can you help me to understand please?

[snip]

I think that Jonathan N. Little may have answered this question in another
post. If it's still not clear to you then I can offer a test for you, which
may not work depending on your settings.

Forewarning: I am not going to comment or explain this bit of php that I am
going to post a link to. If it works great if not then there needs to be
some other way to get it done.

This php script, providing your wamp is set to allow it, will create a
directory, 7 php files, 1 css file, and 1 .htaccess file.

If it works, you will have an example of how all of this can be put to use.

Create a blank php file, then add one include then open it.

<?php include("http://tinyurl.com/289ga2"); ?>

php can also be used to delete or modify files, so you may wish to think
real hard if you really want to try this.

--
BootNic Friday March 14, 2008 8:36 PM
Our earth is degenerate in these latter days; bribery and corruption
are common; children no longer obey their parents; and the end of
the world is evidently approaching.
*Assyrian clay tablet 2800 B.C.*
 
E

Ed Mullen

dorayme said:
Ah, well, that partly explains why you were unable to get my
meaning in the rest. I was hoping the reference to current would
be clear without further explanation because this is what the
thread subject was about. I should perhaps have explained it
more. Here it is then:

There is a practice of making a menu item, to take an example,
look different when a user is on the particular page to which the
menu item is a link to. When the visitor lands on that page, it
is thought helpful for that item to look and even behave
differently (in fact, it no longer needs to be a link just as one
no longer needs to say to the cabbie to take you to such and such
when you are already there <g>).

Let us forget about automation and php for the moment. Think
simple and hand coding. You can achieve your aims by identifying
the "About us" link on the About us page with <li
id="current">...</li>. And you can then put in one simple style
for this id in the css sheet, e.g. color: red; background:
yellow, whereas all other items are blue on white.

Now we get to the question as to whether to id the current or
class it.


There are two perspectives and we must distinguish them. There
are things we do to make the website good and useful to visitors.
And there are things we do to make them manageable to our
webmastering selves. I am talking about the latter. It makes
little difference in the former unless you make a mistake in your
management.

If it is just me making and managing a site, I only use one
current per page. I don't have the practice of highlighting the
nav menu item on the left, and in the footer and maybe elsewhere.
I don't want to get that complicated. i want to bve sure that
there is one and only one style and one item that gets it on the
page. id does it for me. When I validate, I get to pick up any
divergenge from this (eg. 2 same ids on a page).

You want to know, why don't I use class because it "does the same
thing". The short answer is that it does not. It will serve the
visitor fine. But it will serve me worse because when I come to
my css and html months down the track, the id tells me something
that a class cannot tell me, it is a reminder to me that I have
one item only per page that is does something. "O yeah!" I say.

And generally, if it is not for the reason rf mentioned (to be a
handle for some scripting that needs ids), this applies to other
things too, not just "current" items. For example, I use id for
big sections like banner, main nav, wrapper, footer. It is
information for me. It reminds me, it sets the scene for me and
when I validate I am assured they are unique.

I have in the past used class too and have thought about your
question and used to conclude that id was silly until it meant
something for me to use id. I then discovered that it did mean
something for me.


Perhaps you get it a little bit more now? Using id is information
for the webmaster, it tells him or her that there is soethigng
unique going on and that is helpful in some situations. It is not
relevant that some people would like current not to be so unique
(eg. Adrienne's fine point), I am talking about where it is so
wanted.

I am both elucidated and unclear. I kinda get it while not really
getting it. That is, I understand, sort of, your webmaster vs.
web-viewer difference. But, it still doesn't leap to mind, for me, any
example of how I would employ it to my advantage.

Me thinks it is one of those oddities of "standards" construction that
is lost on the common person. And I freely admit that "I don't get it."

Oh well. As that American TV character used to say: "Never mind!"
 
D

dorayme

Ed Mullen said:
I freely admit that "I don't get it."

You get that a validator will flag a fault if there is more than
one id on a page. But not if there is more than one instance of a
class.

So you will get that this is a useful test for an author who
wants only one.

Therefore you will only be wondering why an author might want
only one of something when he can have as many as he likes.

To get this is to penetrate the martian brain. It has never yet
been done and in the following I am going to ensure that the
traditions of the past continue into the future.

How is it useful for an author to be assured that his page is
populated with special clubs that have only one member? Well,
suppose that there is an outbreak of flu on the page or the
website generally. Easy lies the antennae of the martian head
knowing that there is no mingling among the members of the main
clubs, the big wrappers, the main nav, the important if low
footer. These are easy infection control structures.

But mere lower classes! They are disease prone, the members
gather in common clubs and drink low quality brews, you can hear
them slurping ... it is so disgusting. Still, they are needed for
the more menial jobs, one member being as good as another and all
being given the same characteristics...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top