Forum Moderators: coopster

Message Too Old, No Replies

Dynamic Assignment of CSS

Assigning a CSS Selector to Navigation

         

katana_one

3:17 pm on Jan 27, 2006 (gmt 0)

10+ Year Member



I have a simple site I am building, and it will ultimately reside on a server that supports PHP. I do not have any experience at all yet with PHP (any recommendations on a good source of how-tos?).

I want to take advantage of the PHP support to create a template instead of individual pages, and I was wondering how I could assign a CSS selector to my main navigation via PHP based on what page the user is currently viewing.

For example, if the user were currently viewing the "contact" page, the HTML would look something like this:

<div id="nav">
<ul>
<li><a href="index.php">Home Page</a></li>
<li><a href="contact.php" class="tab">Contact Us</a></li>
<li><a href="service.php">Services</a></li>
<li><a href="about.php">About Us</a></li>
</ul>
</div>

Where class="tab" has been assigned dynamically.

I also need to learn how to build and process a fairly long form on this site via PHP, and I need an easy-to-follow "how-to."

jatar_k

5:45 pm on Jan 27, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



normally when I need page/directory specific output in my template I use the path

I break up the path in the top of my template and set it in a var then I can use that var through out my template to make decisions as you mentioned.

take a look at the vars available in $_SERVER [php.net]

as for the processing of forms, take a look through our Library [webmasterworld.com] there are some threads in there that might help, if not, then post what you can't figure out.

katana_one

5:22 pm on Jan 29, 2006 (gmt 0)

10+ Year Member



take a look at the vars available in $_SERVER

Thanks for the reply, but I'm afraid I have no idea what to do with this suggestion. As I stated in my original post, I have no idea what I'm doing at this point.

I'm not asking to be spoon-fed some code that I can copy and paste (although I will accept it) but I'm afraid that most online PHP manuals I've found so far are over my head.

bedlam

6:08 pm on Jan 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Given that you're not really comfortable with php, you may want to consider a CSS-only option for your navigation--provided that it's not too complex.

You start off with this code:

<div id="nav">
<ul id="nav">
<li><a href="index.php">Home Page</a></li>
<li><a href="contact.php" class="tab">Contact Us</a></li>
<li><a href="service.php">Services</a></li>
<li><a href="about.php">About Us</a></li>
</ul>
</div>

...but I'd change it this way:

<!--
Removed <div id="nav"> because the <ul> can almost certainly be styled as you like without the outer div...if I'm wrong, you can just put it back ;-)
-->
<ul>
<!--
Added an id attribute to each menu item:
-->
<li id="homeLink"><a href="index.php">Home Page</a></li>
<li id="contactLink"><a href="contact.php" class="tab">Contact Us</a></li>
<li id="serviceLink"><a href="service.php">Services</a></li>
<li id="aboutUsLink"><a href="about.php">About Us</a></li>
</ul>

Then, you would assign an id to the body element in each of index.php, contact.php, service.php, and about.php respectively like this:

<body id="home">
<body id="contact">
<body id="service">
<body id="aboutUs">

Then, in your stylesheet, you can define the default state for the navigation links in about the same way you probably already have:

#nav { /* Styles for the container (now a ul, not a div */ }
#nav li { /* Styles for the list items inside #nav */ }
#nav li a { /* Styles for the DEFAULT STATE of the links inside the list items inside #nav */ }

Finally, instead of using .tab to define the state of the active menu item, you'd do this:

#home #homeLink a,
#contact #contactLink a,
#service #serviceLink a,
#aboutUs #aboutUsLink { /* Styles for the current link--whichever one it is */ }

Where this method gets cumbersome, of course, is in that last block of code if you have a very large menu. But it has the advantage that (especially if your pages are not fully dynamic) the menu code is the same on every page and only the body element changes.

-b

katana_one

4:23 am on Jan 30, 2006 (gmt 0)

10+ Year Member



Yes, I'm already using unique IDs on all of the body tags, and what you are suggesting would work. But it seems like it would be a more elegant solution to just assign the class selector dynamically. I've done this for another site in ASP with some copy-n-paste ASP script, and I just can't figure out how to translate to PHP.

I may end up doing a pure CSS solution if I cannot figure out how to do it with PHP by launch date.

jatar_k

4:52 am on Jan 30, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



alrighty then

consider this

echo $_SERVER['PHP_SELF'];

this will give you the full url of the page, try it and see.

now you should grab that into a variable like so

$checkit = $_SERVER['PHP_SELF'];

then you can compare your pagename against that if you like. Maybe something like this for any given menu item

echo '<li><a href="contact.php"';
if(strstr($checkit,"contact.php")) echo ' class="tab"';
echo '>Contact Us</a></li>';

should work

katana_one

1:33 pm on Jan 30, 2006 (gmt 0)

10+ Year Member



$checkit = $_SERVER['PHP_SELF'];

Ah. Thank you for the code with explanations of what it is doing. That makes a lot more sense than just the variable name on its own.

Is the "$checkit" part of that line a reserved variable name (or whatever its called) or can I change that to something else if I want?

I am going to try it at lunch or after I get off work this evening. I will let you know how I do.

tc3driver

5:10 pm on Jan 30, 2006 (gmt 0)

10+ Year Member



Is the "$checkit" part of that line a reserved variable name (or whatever its called) or can I change that to something else if I want?

the "$checkit" is a variable that can be called later in the script.


$checkit=$_SERVER['self'];
echo $checkit;

//would printe the same thing as

$whatever=$_SERVER['self'];
echo $whatever;

as for resources php.net is a great resource for function listings, and snippits of code. do a google for php scripts and you will find a large amount of info on the net.

jatar_k

6:23 pm on Jan 30, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> can I change that to something else if I want

yes, I just used that as an example

katana_one

10:53 pm on Jan 30, 2006 (gmt 0)

10+ Year Member



It does work! Thanks for the help. Now hopefully, I can apply what I learned here today to other tasks...

jatar_k

10:57 pm on Jan 30, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



now that is what I love to hear :)

glad to help

katana_one

1:25 pm on Jan 31, 2006 (gmt 0)

10+ Year Member



I'd like to learn PHP (and ASP) but every time I try to read any on-line manuals my vision starts to blur and my head starts to hurt by the second paragraph.

I just can't seem to find "the zone" I need to be in for writing scripts. Waaaaay back in high school, I could write in BASIC almost intuitively. But who uses BASIC anymore?

omoutop

2:27 pm on Jan 31, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi katana_one!

you remind me of myself time ago when I was trying to learn how to create dynamic pages. The only cure for this in my opinion is a good book where you can find lots an lots of theory, examples, and code as well. I would reccommend you to go and buy a good book and read it without worrying if you understand everything at once. Practise makes perfect my friend but a bit of theory just for the start will be the best guide for you. Believe me, php is really simple once you understand its ideology, logic and structure. Soon enough you will be able to write your own scripts and you will be amazed by the time you need to reach such level.
Hope the best

jatar_k

5:42 pm on Jan 31, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> I just can't seem to find "the zone"

you found it, you're here. Just the fact of having other people here to bounce code off of helps a ton. You'll get there don't worry too much about it.