Hello,
I'm having a hard time getting the title of each page to appear in the Browser top bar.

I have followed several tutorial that instruct on how to achieve this result but none work. i know it's because of the somehow odd structure each of my website's pages have. Unfortunately I cannot change it (the site has so many pages it will be madness to change all of them!).

This is the best tutorial I found:
http://www.alistapart.com/articles/keepingcurrent

<?php $thisPage="About Us"; ?>
<head><html>
<title>Company Name<?php if ($thisPage!="")
  echo " | $thisPage"; ?></title>

It advises me to set the page title at the top of each page, before the header is called. I cannot do this because each of my pages is constructed this way, using Smarty templates.

example.php

is created by smarty with the following code:

$main_content = $smarty->fetch('example.tpl');
  	    $smarty->assign("MAIN_CONTENT",$main_content);
      	$smarty->display('main.tpl');

So the first first we look at is the main.tpl template.

main.tpl (simplified for this example)

{php}
     include('header.htm');
{/php}

 <div id="canvas">

  {php}
 include('navigation-user.php');
{/php}

<div id="main">
            {$MAIN_CONTENT}   
</div> {php} include('footer.htm'); {/php} </div> </body> </html>

So the main.tpl page includes the header:

header.htm

!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Title for each page is what I want! </title>

<link href="css/style.css" rel="stylesheet" type="text/css" />
</head><body>


The the maint.tpl template creates the site frame and inserts the content of the example.tpl page (as instructed by Smarty in the example.php page).

This is the structure of each page. For this reason I don't seem the be able to give a unique $thisPage value to each page, seeing as the header is the same for all pages.

For the same reason, I cannot use the instructions published on the smarty website:
http://smarty.php.net/crashcourse.php

<html>
<head>
<title>{$title|default:"no title"}</title>
</head>
<body>
{include file="header.tpl" title="User Info"}

User Information:<p>

Name: {$name|capitalize}<br>
Address: {$address|escape}<br>

{include file="footer.tpl"}

is there a workaround? Some smarter way of achieving the same result?

I'm only a newbie, on my first post here. If you are so kind to help, please do soo in simple words!

Much appreciated, really!
Cheers
manula

    For the sake of clarity rename header.htm to header.tpl (it is not necessary for it to work). Instead of

    {php}
         include('header.htm');
    {/php}
    

    do

    {include file="header.tpl" title="User Info"}
    

    and in your main php file

    $smarty->assign('page_title', choose_title_based_on($_GET['page']));//you need to write this function
    $main_content = $smarty->fetch('example.tpl');
                $smarty->assign("MAIN_CONTENT",$main_content);
            $smarty->display('main.tpl');
    

    now you can use {$page_title} in your header.tpl.

      Thanks mate for your reply.

      Unfortunately I need some other form of trick, because the header.htm (or header.tpl after I rename it) is the same for the entire website. Not much use assigning page name to the header...

      As I said I can't change the structure of the entire website, so I'm looking fot other tricks...

      Thanks again,
      Cheers

        If you insist on it (but it's not very elegant, nor efficient):

        {php}
             $head = file_get_contents('header.htm');
             echo str_replace('<title>Title for each page is what I want! </title>','<title>New title</title>',$head);
        {/php}
        

          Dear php bilder

          I have a problem with the php code. I notice this error when I tried to search from the form page , instead of retrieving the name of the persion, the result of the query came back with this message

          contact_nameSELECT * FROM bzstable WHERE contact_name= "philip"
          Fatal error: Call to undefined function mysql_query() in

          C:\xamppserver\xampp\htdocs\BZS\bzsform.php on line 31
          When I checked line 31 I couldn’t find the error , do you may have any idea
          to fix it.

          Please look further down to the end of this page to see this problem clearly on line 31
          Line $result = mysql_query($query) or die ('query error')

          <body>
          <?php

          /mysql_connect('localhost', 'resoum' 'seyoum')
          or die ('error');
          mysql_select_db("bzstable") or die ('database error');
          /

          /if ($POST['select'] == 'organisation')
          {
          $query = 'SELECT
          FROM bzstable WHERE organisation="'. $POST['optionsvalue2'] . '";';

          } else if ($POST['select'] == 'contact_name')
          {
          $query = 'SELECT * FROM bzstable WHERE contact_name="'. $
          POST['optionsvalue2'] . '";';

          };
          if ($POST['select'] == 'category')
          {
          $query = 'SELECT * FROM bzstable WHERE category="'. $
          POST['optionsvalue2'] . '";';

          };
          if ($POST['select'] == 'country')
          {
          $query = 'SELECT * FROM bzstable WHERE country="'. $
          POST['optionsvalue2'] . '";';

          };*/

          echo "{$POST['select']}";
          $query = "SELECT * FROM bzstable WHERE {$
          POST['select']}= \"{$_POST['optionsvalue2']}\" ";
          echo $query;

          line 31 $result = mysql_query($query) or die ('query error'); LINE 31

          // Printing results in HTML
          while($field = mysql_fetch_array($result, MYSQL_ASSOC))
          {
          echo '<hr>' . $field['orgainsation'] . '<hr>' . $field['contact_name'];
          }

          {
          echo '<hr>' . $field['category'] . '<hr>' . $field['country'];
          }
          //WHERE MATCH (title,body) AGAINST ('database');

          //end //mysql_close. /

          // Closing connection
          // Closing query
          //mysql_close($query);

          ?>

          </body>

            wilku wrote:

            If you insist on it (but it's not very elegant, nor efficient):

            {php}
                 $head = file_get_contents('header.htm');
                 echo str_replace('<title>Title for each page is what I want! </title>','<title>New title</title>',$head);
            {/php}
            

            Where would I put this code mate?
            Thanks

              this still wont work for me.
              The page that I use to call the header is not the actual page (with the content) but a smarty template used to display the content within itself.
              So basically also the page that includes the header is one and the same for the whole site.

              Do I just have to give up the idea of having different page titles because of my (maintainable) site structure?

              Hope not!

                instead of include('header.htm'); in main.tpl.

                  I don't think this will solve the problem.
                  If I do the way you're suggesting I still would have no way of specifying what the title for each page has to be, because the only unique element in the whole site structure is the template with the content. All the rest is the same throughout the site.

                  Basically to have the page contactus.php displayed on the user's browser this happens (as instructed by Smarty coded within contactus.php).

                  main.tpl is called first (same content for the whole site)
                  main.tpl calls the header to be included (same content for the whole site)
                  main.tpl then creates the site structure with html/css (same content for the whole site)
                  main.tpl then calls the contactus.tpl to be included (this is the ONLY unique page and it's the page with the content for the Contact Us page)
                  main.tpl finally calls the footer to be included (same content for the whole site)

                  Basically I need to put an identifier with the page title in contactus.tpl because it's the only component of the whole structure here that is actually unique.

                  The problem is that contactus.tpl is only called into play AFTER the header has been included.

                  I hope I explained it right this time!
                  Thanks a lot for trying to help, it's much appreciated, really.

                    Then include the only unique page before everything else. If it's written as a template, use the ->fetch(), if it's a php file, use [man]ob_get_contents[/man] and the other ob_'s. You'll have your title now, so you can assign it and assign the content of this unique page to another variable.
                    If this isn't going to help you, I'd advice you to seriously reconsider the whole concept.
                    If I do something in Smarty, I work as follows:
                    - do every action that is called, without echoing anything
                    - when everything is done (selecting from database, logging, connecting to other servers etc. whatever this page is supposed to do), I assign template variables and show the template. This way on showing the template from the top you have all the necessary data to fill the template.
                    - another thing is, that the code that I run first tell me which unique template to load inside global template
                    I think it's better, but I suppose this way's drawback is that page doesn't start to show untill everything is done.

                      Write a Reply...