Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 3 123 LastLast
Results 1 to 15 of 33
  1. #1
    Senior Coder ahallicks's Avatar
    Join Date
    May 2006
    Location
    Lancaster, UK
    Posts
    1,134
    Thanks
    1
    Thanked 57 Times in 55 Posts

    Dynamic <title></title> depending on page

    Here's the problem. I would like to generate a dynamic page title for each of the pages on my site. As thing stand at the moment, there is one 'template' page called index.php, which adds content to the page depending on the URL string (what is picked up via $_GET). The problem is that for each of these include pages, if I use a variable called $page_title or something, and echo that variable in the <title></title> of the page, it only works if you visit the page twice because the top section of the code has already been generated and the $page_title is sent after the <title></title> has been printed.

    Is there any way I can dynamically create this title using this method?

    The function:
    PHP Code:
        function make_title($title) {
            if (!
    $title){
                
    $title '';
            }
        } 
    The title:
    PHP Code:
        <title><?php echo $title?></title>
    Setting the variable in each page:
    PHP Code:
    <?php
        $title 
    'Welcome To The Home Page';
    ?>
    "write it for FireFox then hack it for IE."
    Quote Originally Posted by Mhtml View Post
    Domains are like women - all the good ones are taken unless you want one from some foreign country.
    Reputation is your friend

    Development & SEO Tools

  • #2
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    put this on top of your page

    PHP Code:
    if (!isset($_GET['page'])) {
    $variable "home";
    }
    else {
    //warning security risk here ;)
    $variable $_GET['page'];

    define function display title before calling it

    PHP Code:
    function display_title ($variable) {

    if (
    $variable == home):
    echo 
    "home page";
    elseif (
    $variable == dome):
    echo 
    "dome page"
    else:
    echo 
    "home page";
    endif;

    //you can use switch there if you prefer it


    and then just call the f-ion with display_title($variable) inside <title> tags

  • #3
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    That's a really bad solution. Especially because it would generate warnings and it's just really bad form to use echo in a function. The other issue is that as your site grows, you gather up a huge switch block for no real reason.

    You should really look into seperating out the various parts of your site. Think along the lines of a template (without using a templating system) for the main design. Seperate each page into 2 sections, a model and a view. Execute the model before you display anything. It will parse all the incoming data and prepare the data to be displayed. Then as you're displaying the main template, include the view for the current page wherever your template holds the output of the page.

    This allows you to set the title in the model. Which solves the chicken before the egg problem

    A clean way of abstracting this is the following: Using a static class (called UI for example) that holds several generic things about the page. For example, it can contain the Page's title, description, keywords, scripts (JavaScript) and stylesheets.

    This allows the individual pages to add little bits of information to the main page layout without having a lot of ifs and variables laying around.

  • #4
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    ...which means that you need to know quite a lot about coding, if you want to make a really dynamic website...

  • #5
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    PHP Code:
    $page $_GET['page'];

    include(
    'models/' $page '.php');

    include(
    'common/header.php');

    include(
    'views/' $page '.php');

    include(
    'common/footer.php'); 
    A lot? I'm not sure if that qualifies as a lot of coding.

    The static class was optional, but...

    PHP Code:
    class UI {
        private static 
    $title;

        public static function 
    getTitle() { return self::$title; }
        public static function 
    setTitle($value) { self::$title $value; }

    common/header.php:

    PHP Code:
    <html>
    <head>
        <title><?php echo UI::getTitle(); ?></title>
    </head>
    <body>
    models/home.php:
    PHP Code:
    <?php
        UI
    ::setTitle('The Home Page');
    ?>
    views/home.php:
    PHP Code:
    <?php
        
    echo 'Hello World';
    ?>
    common/footer.php:
    PHP Code:
    </body>
    </
    html
    You tell me which of these elements requires a lot of knowledge about coding.
    Last edited by aedrin; 12-05-2007 at 05:44 PM.

  • #6
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    it takes quite time to figure those things out . of course most often it turns out that things you thought were hardest, are simple. well, once when you know something it looks simple, but it's not. it's simple only 'couse you done it thousand times. and you learn new ways while you were repeating same code over and over. that's why i said you need to know a lot about coding. it maybe isn't complicated code, but it sure is hard to get to that level when you know how it's simply done

    these mad smileys aren't for you. they are for me, they express those times when i just don't know how to do something, that's why i put so many.
    Last edited by matak; 12-05-2007 at 05:54 PM.

  • #7
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Writing better code comes from knowing your tools.

    Most of the things I figured out, not from writing a thousand pages and then realizing how it could be better. It comes from writing a single page, realizing there is a problem and researching what I can do to improve this.

    The simplest and most efficient way of improving this is to go to www.php.net and read.

    Read up on:

    - PHP's Object Oriented Programming
    - All the different ways to manipulate arrays
    - How to work with databases

    Now you understand a lot more of these concepts, and thus have the tools. Now you need to use these tools.

    Sometimes you will figure out yourself how to use them together to solve your problem, sometimes someone else has figured out a different problem but you realize it also works for your problem.

    I'm not a big fan of just using someone else's code. Instead I read their code, extract the techniques that I desire and implement my own (lean and generic) version.

    Nothing will improve your skills more than going out there and figuring out your own problems. Taking a class teaches you how to use the tools and solve problems according to one way. Once you come across a new problem, you're stuck.

    This is the difference between a self-taught programmer and a taught (in classes) programmer. The first knows how to solve new problems, because that is one of the most important skills as a programmer. The second knows how to solve a set of problems, and will use this limited set of tools to solve the problem. The job is done, but not as cleanly. Over time, the taught programmer will figure things out but in the end it will have taken more time.

  • #8
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,858
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Quote Originally Posted by aedrin View Post
    PHP Code:
    $page $_GET['page'];

    include(
    'models/' $page '.php');

    include(
    'common/header.php');

    include(
    'views/' $page '.php');

    include(
    'common/footer.php'); 
    So, how can I include some specific external CSS/JS files for a particular page- say, a page which contains a RTE. (Just reached here from your reference in Cannot modify header information)
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #9
    Senior Coder ahallicks's Avatar
    Join Date
    May 2006
    Location
    Lancaster, UK
    Posts
    1,134
    Thanks
    1
    Thanked 57 Times in 55 Posts
    Aedrin, thanks a lot for yuor help. I've actually just bought two books on OOP from Sitepoint that I'm about to work through. The only training I had with php was a book meant for beginners, the rest I've sort of found and modified from the web (and learnt along the way). I think, with this site, I've come a little too far to gut it and start again with that method, but I'll certainly be incorporating what you have told me here.

    For this problem I'll just stick to getting the page title from the get of the section that is included in the page as I have ben doing. It's not description and friendly, but it's the name of the page and different for each.

    Thanks again! *heads to php.net again for reading time*
    "write it for FireFox then hack it for IE."
    Quote Originally Posted by Mhtml View Post
    Domains are like women - all the good ones are taken unless you want one from some foreign country.
    Reputation is your friend

    Development & SEO Tools

  • #10
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Quote Originally Posted by abduraooft View Post
    So, how can I include some specific external CSS/JS files for a particular page- say, a page which contains a RTE. (Just reached here from your reference in Cannot modify header information)
    Update the UI class to be like this:

    PHP Code:
    class UI {
        private static 
    $title;
        private static 
    $stylesheets = array();

        public static function 
    getTitle() { return self::$title; }
        public static function 
    setTitle($value) { self::$title $value; }

        public static function 
    addStylesheet($stylesheet) { self::$stylesheets[] = $stylesheet; }
        public static function 
    getStylesheets() { return self::$stylesheets; }

    Then update header.php as such:

    PHP Code:
    <html>
    <head>
        <title><?php echo UI::getTitle(); ?></title>
        <?php
            $stylesheets 
    UI::getStylesheets();
            foreach (
    $stylesheets as $stylesheet) {
                echo 
    '<link href="css/'.$stylesheet.'" rel="stylesheet" type="text/css" />';
            }
        
    ?>
    </head>
    <body>
    The same code applies for script files. Just use another array, with 2 extra methods.

    Now in your model you can say:

    PHP Code:
    <?php
        
    if (/* need RTE */) {
            
    UI::addStylesheet('rte.css');
            
    // etc.
        
    }
    ?>

  • #11
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,858
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Many thanks aedrin, (but no way to add reputation or give thanks )
    I'm going to practice this method!
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #12
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Quote Originally Posted by abduraooft View Post
    (but no way to add reputation or give thanks )
    I believe using the little scales to the left of a post still work (the middle icon of the 3).

  • #13
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,858
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Quote Originally Posted by aedrin View Post
    I believe using the little scales to the left of a post still work (the middle icon of the 3).
    Nop, it says me to spread. (the same happens when I try for many people here!)
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #14
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Then you've given too many thanks currently :P

  • #15
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,858
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Hi aedrin, one more doubt
    How can I add internal style/script with this UI class?
    (sorry ahallicks, for cross posts in your thread)
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

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