Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Highlighting a dynamic php page in the menu - have code

  1. #1
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default Highlighting a dynamic php page in the menu - have code

    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 Code:
    <?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.

  2. #2
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default

    You could make your menu like this:

    Code:
    $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>");
    }
    Last edited by motormichael12; 07-27-2008 at 03:51 AM.

  3. #3
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Try this:

    PHP Code:
    <?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.
    Edit: sorry motormichael, was typing it out when you posted. Also,

    Code:
    $location = $GET['page'];
    should be:

    Code:
    $location = $_GET['page'];
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  4. #4
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default

    No worries, just means we both got an idea

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

  5. #5
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    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 Code:
    <?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.
    Last edited by ?foru; 07-27-2008 at 04:23 AM.

  6. #6
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default

    This is one of the first scripts I created, it is to get those cool ?page= urls

    Code:
    <?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.
    ====================================================================================

  7. #7
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    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.

  8. #8
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default

    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

  9. #9
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    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

  10. #10
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default

    If you notice, things that produce multiple query strings are fine
    Look at this: http://www.google.com/search?client=...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 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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •