title: Dynamically change the title of the page with PHP
body: <p>I have a blog and I would like the  tag in the head section of the HTML document to change dynamically using PHP. Here is my PHP code:</p>
  
  <pre><code>&lt;?php 
      session_start();
  if(isset($_SESSION['admin']) and !empty($_SESSION['admin'])){
  
  
  
  
  
      echo '&lt;a href="logout.php" style="color:yellow;"&gt;Logout&lt;/a&gt;';
  
  
  }
  
      require('mysql2.php');
  
  
  
  
  
          //Displaying the information from the DB to the user
          //with pagination, i.e different pages
  
              function post(){
  
  
              $sp_query = mysql_query("SELECT COUNT(id) FROM posts");
              $pages = ceil(mysql_result($sp_query, 0) / 1);
  
  
          $page = (isset ($_GET['page'])) ? (int)$_GET['page'] :     1;   
  
  
              $start = ($page - 1) * 1;
  
  
  
              $query = mysql_query("SELECT id,title, article, datetime  
  
  
    FROM posts ORDER BY id DESC LIMIT $start, 1 ");
  
  
              //Check if there are any rows in the DB
                      if(mysql_num_rows($query)){
  
  
                  while($result = mysql_fetch_assoc($query)){
  
              //displaying the results
  
  
                          echo '&lt;article class="blog-post"&gt;
                          &lt;div class="blog-info"&gt;
  
  
  
   '.stripslashes($result['title']).'  ';
  
  
  
                          echo  
  
   stripslashes($result['datetime']).'&lt;div&gt;';
  
  
  
                          echo 
  
   nl2br(stripslashes($result['article'])).'&lt;/article&gt;';
  
  
  
  
                          $title[] = $result['title'];
  
  
                          $id = 
  
  mysql_real_escape_string((int)$result['id']);
                          echo '&lt;input type="hidden" 
  
  value="'.$result['id'].'" /&gt;';
                          $_SESSION['post_id'] = $id;
  
  
              //If the admin is logged in, session variables
              //for editing and deleting a post must be set
              //and the conforming links should be displayed
  
                          if(isset($_SESSION['admin']) and 
  
   !empty($_SESSION['admin'])){
  
  
  
  
  //$_SESSION['post_id'] = $id;
  
                          $_SESSION['edit_post'] = $id;
  
                          echo '&lt;article class="blog-post"&gt;';
                              echo '&lt;a href="delete.php" 
  
  
    id="delete" onclick="return del();"&gt;Delete this post&lt;/a&gt;';
                              echo '&lt;br /&gt;&lt;a 
  
     href="edit_post.php"&gt;Edit this post&lt;/a&gt;';
                          echo '&lt;/article&gt;';
                  }
                  }   
                  }
  
          //The drop down menu 
  
              function options(){
  
                  $new_query = mysql_query("SELECT title FROM posts 
  
   ORDER BY id DESC");
              $num = 1;
                  while($array = mysql_fetch_assoc($new_query)){
  
  
                      echo '&lt;option 
  
    value="blog.php?page='.$num++.'"&gt;'.$array['title'].'&lt;/a&gt;&lt;/option&gt;';
  
                  }
              }
  
  
  
          ?&gt;
  </code></pre>
  
  <p>And here is the HTML:</p>
  
  <pre><code>   &lt;?php require('mysql2.php');
      require('blog_process.php');
          ?&gt;
  
   &lt;!DOCTYPE html&gt;
    &lt;html&gt;
    &lt;head&gt;
      &lt;!--Meta Data--&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;meta name="description" content="About Chris Shilts"&gt;
      &lt;meta name="author" content="Chris Shilts"&gt;
      &lt;meta name="keywords" content="chris, shilts"&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1,    
  
                 maximum-scale=1"&gt;
      &lt;meta http-equiv="X-UA-Compatible" content="IE=9" /&gt;
      &lt;!--css--&gt;
      &lt;link rel="stylesheet" href="reset.css"&gt;
      &lt;link rel="stylesheet" href="style.css"&gt;
  
      &lt;!-- Javascript --&gt;
      &lt;script type="text/javascript" src="delete.js"&gt;&lt;/script&gt;
      &lt;!-- Favicons--&gt;
      &lt;link rel="shortcut icon" href="favicon.ico"&gt;
      &lt;!--Title--&gt;
  
      &lt;title id="title"&gt;&lt;?php //php code should go here?&gt;&lt;/title&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
      &lt;!--Contains all content--&gt;
      &lt;div id="container"&gt;
          &lt;!--Content at start of page, generally the same--&gt;
          &lt;header&gt;
              &lt;div id="logo"&gt;
              Hello There!
              &lt;/div&gt;
              &lt;!--Primary Navigation--&gt;
              &lt;nav&gt;
              &lt;a href="#"&gt;Link&lt;/a&gt;
              &lt;a href="#"&gt;Link&lt;/a&gt;
              &lt;a href="#"&gt;Link&lt;/a&gt;
              &lt;/nav&gt;
          &lt;/header&gt;
  
          &lt;!--Blog Posts--&gt;
  
          &lt;?php post();?&gt;
  
  
      &lt;!-- The navigation bar for the blog posts --&gt;
  
     &lt;select onclick="navigation();" id="select"&gt;
  
      &lt;option value="" selected="selected"&gt;&lt;/option&gt;
  
      &lt;?php options(); ?&gt;
  
      &lt;/select&gt;
  
  
          &lt;!--First Footer--&gt;
          &lt;footer id="footer-one"&gt;
          Site Design By &lt;a href = "#"&gt;Chris Shilts&lt;/a&gt; | Programming by   
  
      &lt;a     href = "#"&gt;Stefany Dyulgerova&lt;/a&gt;
          &lt;/footer&gt;
          &lt;!--Second Footer--&gt;
          &lt;footer id="footer-two"&gt;
              &lt;a href="http://www.w3.org/html/logo/"&gt;
              &lt;img id="html5" src="http://www.w3.org/html/logo/badge 
  
  
  
  
     with CSS3 / Styling"&gt;
              &lt;/a&gt;
          &lt;/footer&gt;
  
  
          &lt;!--/container--&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I have also have a mysql database and here are the fields there:</p>
  
  <pre><code>id title article dateime
  </code></pre>
  
  <p>Please help me!</p>
  
  <p>I tried to put the $result['title'] into a an array like this</p>
  
  <pre><code>$title[] = $result['title'];
  </code></pre>
  
  <p>And then to loop it through in the  element based on the based on the id in the $_SESSION['post_id'] but the problem is that I have to reload the page twice in order the title to take effect.</p>