Forum Moderators: coopster
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."
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.
take a look at the vars available in $_SERVER
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.
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
I may end up doing a pure CSS solution if I cannot figure out how to do it with PHP by launch date.
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
$checkit = $_SERVER['PHP_SELF'];
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.
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.
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?
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