SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dynamic header's code sequence

    Hi, I m making a page that has a dynamic header (title, meta description blah) corresponds to the Mysql query result

    i check if the url sets an id, if it doesnt then the page is a "homepage" and it has a homepage title, else the title is the name on the data entry

    Since i query (mysql_fetch) later on in the page to get the page content, do i absolutely need to have another query in the first place to display the headers?

    here's what i am talking about in codes:

    what i think might work:

    Code PHP:
    <!-- http stuff here -->
    <header>
    <title><?php echo "$page_title"?></title>
    <meta description = php here>
    </head>
     
    <?php 
     
    if !isset($_GET['id']) {
    //static title
    $page_title = "HOMEPAGE Z0mg!"
    ...some query to display a list of shortened version of contents..
    }
     
    else {
    $id = $_GET['id'];
    $query = "SELECT pid, title, content stuff blah blah FROM blab";
    $result = @mysql_query($query);
    $row = mysql_fetch_assoc($result);
    //set page title here?
    $page_title = $row['title']
    //then display content
    blah.

    i dont know how php sequences its code. Due to some odd reasons, i dont have the environment to test these codes out ( i will be able to, eventually)

    but if someone can explain what's happening here, that would be awesome.

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    No, that won't work. You have to simply move the code above the html output. Once $page_title is echoed, you're not gonna change the output later on.
    Saul

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by php_daemon View Post
    No, that won't work. You have to simply move the code above the html output. Once $page_title is echoed, you're not gonna change the output later on.
    sigh

    oh well one more line of php wont hurt anyone

  4. #4
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Well, you can always do this:
    Code HTML4Strict:
    <?php
    if (!isset($_GET['id']) {
      //static title
      $page_title = "HOMEPAGE Z0mg!"
      //...some query to display a list of shortened version of contents..
    }else {
      $id = $_GET['id'];
      $query = "SELECT pid, title, content stuff blah blah FROM blab";
      $result = @mysql_query($query);
      $row = mysql_fetch_assoc($result);
      //set page title here?
      $page_title = $row['title'];
      $content = $row['content'];
    }
    ?>
     
    <html>
    <head>
    <title><?php echo $page_title; ?></title>
    <meta description = "<?php echo $page_description; ?>" />
    </head>
    <body>
    <?php echo $content; ?>
    </body>
    </html>
    Or better yet, move the html to a different file and include it in php script. Let's call the html file template.php:
    Code HTML4Strict:
    <html>
    <head>
    <title><?php echo $page_title; ?></title>
    <meta description = "<?php echo $page_description; ?>" />
    </head>
    <body>
    <?php echo $content; ?>
    </body>
    </html>
    and
    Code php:
    <?php
    if (!isset($_GET['id']) {
      //static title
      $page_title = "HOMEPAGE Z0mg!"
      //...some query to display a list of shortened version of contents..
    }else {
      $id = $_GET['id'];
      $query = "SELECT pid, title, content stuff blah blah FROM blab";
      $result = @mysql_query($query);
      $row = mysql_fetch_assoc($result);
      //set page title here?
      $page_title = $row['title'];
      $content = $row['content'];
    }
     
    include('template.php');
    ?>
    Saul

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by php_daemon View Post
    Well, you can always do this:
    Code HTML4Strict:
    <?php
    if !isset($_GET['id']) {
    //static title
    $page_title = "HOMEPAGE Z0mg!"
    //...some query to display a list of shortened version of contents..
    }else {
    $id = $_GET['id'];
    $query = "SELECT pid, title, content stuff blah blah FROM blab";
    $result = @mysql_query($query);
    $row = mysql_fetch_assoc($result);
    //set page title here?
    $page_title = $row['title'];
    $content = $row['content'];
    ?>
     
    <html>
    <head>
    <title><?php echo $page_title; ?></title>
    <meta description = php here>
    </head>
    <body>
    <?php echo $content; ?>
    </body>
    </html>
    Or better yet, move the html to a different file and include it in php script. Let's call the html file template.php:
    Code HTML4Strict:
    <html>
    <head>
    <title><?php echo $page_title; ?></title>
    <meta description = php here>
    </head>
    <body>
    <?php echo $content; ?>
    </body>
    </html>
    and
    Code php:
    <?php
    if !isset($_GET['id']) {
    //static title
    $page_title = "HOMEPAGE Z0mg!"
    //...some query to display a list of shortened version of contents..
    }else {
    $id = $_GET['id'];
    $query = "SELECT pid, title, content stuff blah blah FROM blab";
    $result = @mysql_query($query);
    $row = mysql_fetch_assoc($result);
    //set page title here?
    $page_title = $row['title'];
    $content = $row['content'];
     
    include('template.php');
    ?>
    yep. thanks for the tips

    i thought of this in the first place

    so php does execute code in a sequence of the way it's written eh

  6. #6
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kidwithshirt View Post
    so php does execute code in a sequence of the way it's written eh
    How else would you expect? It definitely does not jump back and forth randomly.
    Saul


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
  •