Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP Nav with Titles

Options
  • 07-04-2007 6:49pm
    #1
    Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭


    Hey,

    Im currently in the final stages of a small website and wanted to use PHP Nav. Iv seen many ways of it being done, but I searched Google and found a tutorial on the popular tutorial site Zymic (http://www.zymic.com/view_tutorial.php?id=96).

    At present, I divided my pages to make life easier. Iv a header and footer file which is PHP included into all page contents. Each page has a variable $pagetitle declared at the top (which obviously has the page title) and the header reads this variable and changes it as appropriate (within my title tags iv a print $pagetitle statement). This worked with the standard pages, until I added PHP Nav - which because of the way its coded doesnt allow for making life easier.

    Can anybody suggest anyways to fix the problem, or do I need to switch to a more complex PHP Nav system?


Comments

  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    I would imagine you can still do it quite simply... Try this:

    [php]
    <?php
    switch ($HTTP_GET_VARS[id]) {
    //Default - case
    default:
    $title="News";
    include "news/news.php";
    break;

    //Fonts - case
    case 'resources=fonts':
    $title="Fonts";
    include 'resources/fonts.php';
    break;

    //Scripts - case
    case 'resources=scripts':
    $title="Scripts";
    include 'resources/scripts.php';
    break;
    }
    ?>
    [/php]

    etc.


  • Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭Sully


    Hi Mirror,

    Thanks for the attempt, but it doesnt seem to work. Id imagine thats because the header.php file is loaded before the page is, and so the declaration of $title is just ignored.

    Here is the top few lines of the header file (to give you an idea of how it works)..
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    <head>
    <title> <?php print($pagetitle); ?> </title>
    

    and my index file (ie the code from the tutorial) was similar to yours - obviously I used $pagetitle and not $title. I had a php include for header.php before the switch and include footer after the last statement in the file.

    I also attempted adding;

    [php]include "header.php";
    include "footer.php";[/php]

    after the inclusion of the page. So for example;

    [php]//Scripts - case
    case 'resources=scripts':
    include 'resources/scripts.php';
    include 'header.php';
    include 'footer.php';
    break;
    }
    [/php]

    But that just messed up the page layout completely. Any other suggestions? Thanks.


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Of course, sorry, I didn't think that through properly! I know exactly what you're talking about though, I use the same structure for most of the websites I build i.e. header, footer and title variable for each page.

    If your website wont be changing drastically that often (i.e. there wont be hundreds of pages) you could use a get url function at the top of the header.php script and use a case structure to define the title perhaps? Assuming you're urls are relative, or something like /index.php?page=home etc.

    Or maybe use a function or global variable, though I'm not sure on the exact workings of such a method off the top of my head... I might have a tinker with it if I get a chance and get back to you.


  • Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭Sully


    I wont be having anymore pages I dont think (from what I can tell) but its likely that the pages will get a face lift or new content of course, every so often.

    URLs are the same, only thing that changes is whats after ?page=. Not to sure how to approach the getURL Fucntion or similar - im not that good with PHP. Still learning bits as I need them ;)

    Cheers for your assistance so far. :)


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Ok here's something I just tried. Use two switch statements, one for the page title and one for the page selection. Example:

    Header code, for title, place at very top of header:
    [php]
    <?
    //the following line basically reads the url, so the url looks something like
    //www.yoursite.com/index.php?page=Interior

    $page=$_GET;

    switch($page) {

    case("Interior"):
    $title="Interior";
    break;

    case("Location"):
    $title="Location";
    break;

    case("Contact"):
    $title="Contact & Rates";
    break;

    case("Amenities"):
    $title="Amenities";
    break;

    default:
    $title="Home";
    break;
    }

    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    //etc...
    [/php]

    Index switch, which is already working I assume. If you have any trouble with understanding this google 'php get'. Hope this helps


  • Advertisement
  • Closed Accounts Posts: 1,200 ✭✭✭louie


    Mirror's example is very good if you want to change the title from what is passed in the querystring page=?? otherwise for simplicity you can do this:
    [php]
    $title = "Home";//set $title
    //get querystring
    if(!empty($_GET)){
    $title = ucwords($_GET);//capitalise first letter
    $title = str_replace("_"," ",$title);//replace _ with spaces
    }
    /*
    example of passing page in the url
    www.domain.com?page=home_page
    better to use _ then blank space
    */
    [/php]


  • Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭Sully


    Mirror wrote:
    Ok here's something I just tried. Use two switch statements, one for the page title and one for the page selection. Example:

    Header code, for title, place at very top of header:
    [php]
    <?
    //the following line basically reads the url, so the url looks something like
    //www.yoursite.com/index.php?page=Interior

    $page=$_GET;

    switch($page) {

    case("Interior"):
    $title="Interior";
    break;

    case("Location"):
    $title="Location";
    break;

    case("Contact"):
    $title="Contact & Rates";
    break;

    case("Amenities"):
    $title="Amenities";
    break;

    default:
    $title="Home";
    break;
    }

    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    //etc...
    [/php]

    Index switch, which is already working I assume. If you have any trouble with understanding this google 'php get'. Hope this helps

    Thanks a million, that worked perfectly. :) Cheers.

    louie; Thanks for your contribution, but im not to sure what your suggesting. Any chance of explaining a little further, as id love to know your method of doing things - just for learning :)


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    No problem Sully, glad to help!

    Just to let you know, what louie is showing you is how to work with the page name, rather than use a case statement, which in honesty is probably a better method, it takes the url e.g. index.php?page=home_page and would grab the part in bold, replace the underscore with a space, capitalise the first letter of each word and use the result i.e. Home Page as the title for the page. Get me? You should play with it and see what you can do, always the best way to learn!


  • Closed Accounts Posts: 1,200 ✭✭✭louie


    My option is probably better as you only have to make changes to the url querystring and add remove words as you create them, depending on the situation, and you don't have the edit the switch statement to add the words that will match the QS.


  • Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭Sully


    Hi again,

    Ah I see what your saying. That would be a simpler way, but would really depend on how your pages are setup. For example, my "gallery" page is called gallery.php but I want the title page to be more then just "Gallery".

    Here is how my titles work;
    Apartment Hilary - Luxury Apartment - Sutivan, Croatia - PAGE NAME

    so for gallery.php the tile would be;
    Apartment Hilary - Luxury Apartment - Sutivan, Croatia - Photo Gallery.

    Unless of course, I am mis-understanding the statements completely. I assume whats in side Case() must match that of the file name. So like "Interior" the file has to be called Interior.php (it seems to be case sensitive, so I found on fooling around).

    Have I got the right idea, or am I going the wrong direction?


  • Advertisement
  • Closed Accounts Posts: 1,200 ✭✭✭louie


    I don't think you are going in the right direction.
    if you want the page title on gallery.php to be

    Apartment Hilary - Luxury Apartment - Sutivan, Croatia - Photo Gallery.

    then the link for that page should be

    gallery.php?page=apartment_hilary_-_luxury_apartment_-_sutivan_croatia_-_photo_gallery

    or you can check the page itself and set the title then as:

    [php]
    if(stristr($_SERVER, "gallery.php")) $title = "Apartment Hilary - Luxury Apartment - Sutivan, Croatia - Photo Gallery";
    [/php]

    there are many ways you can do this, you just have the choose the one that suit you.


  • Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭Sully


    The current way I have it works fine - I was just making an observation that your code wouldnt suit my needs (I think) from my understanding of how it works :)

    Thanks tho. Your help is very much appreciated.


Advertisement