PDA

View Full Version : Highlighting a dynamic php page in the menu - have code



?foru
07-27-2008, 03:34 AM
The project I am working on uses query strings to supply the pages like...

index.php?page=about

Normally nothing indicates what page in the menu is currently active, but on this project I would like to do this. (using red as an example)


<?php
$request = $_SERVER['REQUEST_URI']; //set the variable $lasturl to the, well, last url.

if (strpos('domain.com/index.php?page=about',$request)) {

echo("<font color=\"red\"<b>About Us</b></font>");
}
else { echo("<a href=\"?page=about\" class=\"menu\">About Us</a>"); }
?>

The above worked fine I thought...until I went to just "'domain.com/index.php" and both menu options I setup with this as a test were highlighted. I am thinking something needs to be added to the $request var, but if the index page is accessed without any query strings I would like to not show anything highlighted until that actual page shows in the URL.

I am partially there until I discovered this issue, so hopefully there is a way I can accomplish this somehow. Thank you very much for any help on getting this resolved.

motormichael12
07-27-2008, 03:42 AM
You could make your menu like this:


$location = $_GET['page'];

// Main (Index) Page
if (empty($location)) {
echo("<font color=\"red\"<b>Index</b></font>");
}else{
echo("<a href=\"index.php\" class=\"menu\">Index</a>");
}

// About Us Page
if ($location == "about") {
echo("<font color=\"red\"<b>About Us</b></font>");
}else{
echo("<a href=\"?page=about\" class=\"menu\">About Us</a>");
}

thetestingsite
07-27-2008, 03:45 AM
Try this:



<?php
$request = $_SERVER['REQUEST_URI']; //set the variable $lasturl to the, well, last url.
if (isset($_GET['page']) && $_GET['page'] == 'about') { /* see if ?page is present and if it is equal to "about" */

echo '<span style="color: red; font-weight: bold;">About Us</span>';
}

else {
echo '<a href="?page=about" class="menu">About Us</a>';
}
?>


Hope this helps.
sorry motormichael, was typing it out when you posted. Also,


$location = $GET['page'];

should be:



$location = $_GET['page'];

motormichael12
07-27-2008, 03:53 AM
No worries, just means we both got an idea :)

And I knew that I just forgot to put the little underscore :O

?foru
07-27-2008, 04:18 AM
motor,

now I know why your name is motor with the fast reply...LOL This board is NOTHING short of amazing...thank you both!! If you did, testingsite thank you for deleting the extra post, as it was absoulutly not intended. I think you both replied too fast, that when I posted and ran down to grab another harpoon IPA, it didn't look like it actually submitted. I really appreciate the rapid responses, and this is sort of common in php so hopefully it will help others.

In the hopes that it will help anyone else, or maybe one of you, the following code is nice and simple to serve pages from another directory...


<?php

$target = isset($_GET['page'])?$_GET['page']:'index';
if ( file_exists('./includes/' . $target . '.php') )
include './includes/' . $target . '.php';
else
include './includes/404.php';?>

Just change "includes" to your directory name and change php to whatever your file extensions are in that directory, that way you can keep things clean on the server side...you store all your files in that directory and it uses the query string method called in the URL.

Many thanks again to you both, and I may be posting shortly a simple stats system that I built which may also be helpful to the users of this board.

motormichael12
07-27-2008, 04:36 AM
This is one of the first scripts I created, it is to get those cool ?page= urls :)


<?php

/*
* PHP SITE FOR THE PHP KNOW-NOTHING!
* Created by Opalelement, who can be
* found anywhere on the internet.
* Google me.
*
* In the code there are 3 options for
* setting the URLs up. It is best to
* choose one and keep it throughout
* the site.
*
* MAKE SURE THAT YOU SAVE THIS FILE
* AS A PHP FILE. IF IT IS SAVED AS
* HTML IT WILL DISPLAY THE CODE.
* YOU CAN HOWEVER RENAME THIS FILE.
*
* ============================
* Anything surrounded by equals
* signs like this is HTML that
* will appear. Feel free to
* use and redistribute this.
* ============================
*/
?>

====================================================================================
This is your header. It will appear on any of the following methods.
====================================================================================

<?php
if(!$_SERVER['QUERY_STRING']) {
// This is your main page, such as index.php or http://site.com/
?>

====================================================================================
This is your index.
====================================================================================

<?
} elseif ($_SERVER['QUERY_STRING'] == "about") {
// This method will be any URL such as http://site.com/index.php?about or http://site.com/?about
?>

====================================================================================
HTML of the About page (do not include header and footer code)
====================================================================================

<?
} elseif ($_GET['act'] == "rules") {
// This will make something like http://site.com/?act=rules or http://site.com/index.php?act=rules
?>

====================================================================================
HTML or rules page (no header or footer HTML... again...)
====================================================================================

<?
} elseif ($_GET['goingto'] == "join") {
// This shows that it doesn't have to be ?act=, it can be anything... ?action= or ?do= or whatever you please
?>

====================================================================================
I won't even tell you what goes here and what not to include, you should know by now.
====================================================================================

<? } ?>

====================================================================================
All footer HTML should go here.
====================================================================================

?foru
07-27-2008, 04:46 AM
motor,

That is the beauty of PHP....in that there are multiple ways of doing things. Thanks for posting, and I will absolutely check it out. Some advise against the quesry strings, but any project I have ever worked on is easily found in the search engines.

motormichael12
07-27-2008, 04:53 AM
The main reson people say not to is because people do the things that pull it and make fake searches, but google and yahoo are starting to find these... I believe they manually check any site that has too many results for a page like that. As long as you are using them for real, you should have no problems.

EDIT oh btw in that script you notice it says created by opalelement, that is me :)

?foru
07-27-2008, 05:05 AM
Very true... the major search engines are catching on to the latest techinques. On my site I haven't had any problems with the quesry strings as I think it is more in how you develop specific meta tags for pages.

btw master developers are known by many names...haha

motormichael12
07-27-2008, 12:42 PM
If you notice, things that produce multiple query strings are fine
Look at this: http://www.google.com/search?client=opera&rls=en&q=Highlighting+a+dynamic+php+page+in+the+menu+-+have+code'&sourceid=opera&ie=utf-8&oe=utf-8

Very first one is this topic :) Its the ones that act as search engines, like this:

Well I was going to give an example but Google is too good now so I can't find any :D but they take what you searched for on google and turn it into their own search engine, and its really annoying to be looking for something and click a link only to find more search links that are alot worse.

?foru
07-28-2008, 01:30 AM
Good point on google picking this thread up already. Who needs any other search engine when you have google :D

motormichael12
07-28-2008, 01:51 AM
Well I think that was a coincidence on timing of the post and google indexing lol

?foru
07-30-2008, 01:59 AM
Yeah, a little too ironic :) I don't work on many sites that use seperate pages like about.php contact.php and so on, but I have one other quick question.

I would like to see if that code could be changed to work in the same way so one include file can be inserted and for instance...keywords, descriptions, so on could be supplied based on domain.com/about.php

I tried to mess around with the previous code to get the request of a static page like about.php but didn't have any luck.

A general idea...
$request = $_SERVER['REQUEST_URI'];

somehow check that if $request equals domain.com/about.php
echo "keyword, keyword 1, keyword 2"; (can be anything really...specific to that page..graphics etc.)

Of course you can have http, https, w/ or w/o www. so domain.com/page.php would cover most. Thank you for any help, just a little curious.