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.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2011
    Posts
    42
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Declaring a variable before it is set..... output buffering??

    I want to use a variable in the <title></title> to display the name of the article being shown on the page.

    Each page uses an included header and footer file, and output buffering is being used.

    So each page starts like this..

    PHP Code:
    <?
    $page_title 
    "Page Title";
    include 
    'header.php';
    include 
    'footer.php';
    ?>
    ...and header.php echos out the $page_title in the title tag.

    However I want the $page_title varible to reflect the actual name of the article, which is set dynamically in the $article_title variable further down the page. Something like..

    PHP Code:
    <?
    $page_title 
    $article_title;
    include 
    'header.php';
    $article_title $result['title'];
    include 
    'footer.php';
    ?>
    But of course due to the way I'm including the header this needs to be put before the variable is actually set causing the problem.

    I already use output buffering, is there a way i can use this to help? This has to be quite a common problem I think...

    Thanks in advance! This has been a problem to me for a while and I can't figure out the best way around it!!

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Quote Originally Posted by aedrin View Post
    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
    See http://codingforums.com/php/129123-dynamic-title-title-depending-page.html
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,338
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    Output buffering simply stops headers being sent. It doesn't allow you to change what has already been printed to the HTML - as far as I know anyway.

    What you need to do, is seperate your html and PHP. Run the php FIRST and then put all the generated content into the html.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #4
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    post the contents of your header.php file here... there will be a way for it to work..


  •  

    Posting Permissions

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