0

The script below echos the array and its individual elements fine but when the array elements are used to set the page title by running the script after the opening head tag, I still get "untitled document" as the page title.

Further if I try echoing $title alone and placing the title tags <> before and after the php tags, the title is set as the document type definition..

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

It seems that echo sets the relevant HTML tags required before if not already set. Whats the work around ??

<?php
include 'contentStream.php' ;
$upc = $_GET['upc'];
if (isset($upc))
{
global $upc ;
$query = "SELECT * FROM tracks WHERE album_upc='$upc'";
connect();
$db = mysql_select_db("XXXXX");
$results = mysql_query($query, $connection) ;
$result = mysql_fetch_assoc($results);
$title = $result['title']." by ".$result['author'] ;
echo "<title>".$title."</title>";
unset($results);
unset($query);
mysql_close($connection) ;
}
else
{
    echo "<title> MYsUPERsITe </title> " ;
}
?>
varun Kishnani
  • 169
  • 1
  • 4
  • 16
  • 3
    Are you sure you are putting this inside the `` tags in your HTML? – Nick Dec 06 '12 at 14:01
  • <?php echo $title; ?> – Nikolay Baluk Dec 06 '12 at 14:04
  • 1
    You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Dec 06 '12 at 14:08
  • Fully realize that Quentin, just that I started very recently and I have a lot of concepts to learn and practice, to get them on my fingertips, I just can't get the time at the moment to relearn on things I can already effect. – varun Kishnani Dec 06 '12 at 14:24

3 Answers3

2

Is this being executed within the <head> element? If not it needs to be.

I am not sure what contentstream.php is but it looks like you might be running this at the wrong location in the page.

Also try changing $title to $myTitle to see if $title has been set somewhere else.

cowls
  • 24,013
  • 8
  • 48
  • 78
  • I forgot to say... contentstream.php contains is a list of functions only... it contains the connect() function, and a lot of other functions not relevant here... it dosen't output a thing... Ill try without including that and get back in a second. – varun Kishnani Dec 06 '12 at 14:08
  • Much thanks cowls, I took care to remove the opening HTML,HEAD, TITLE AND BODY tags in contentStream.php but the closure tags for each of these remained... – varun Kishnani Dec 06 '12 at 14:11
1

Your problem seems to be that you are trying to change the contents of the <title> tag after it has already been produced by the non-PHP section of your page. That approach does not work in a server-side environment like PHP.

One solution is to use PHP to generate the entire HTML page, store it in a variable, and just echo() it out when you're all done.

At the top of your page, I would run and parse a database query and build a $html string to echo out later. Multiple echo() statements like you have above can get ugly, and can give you issues with headers and such if you later end up adding cookies or session variables to your site:

<?php
    include ("my_cool_lib.php");
    $html = "<!DOCTYPE html>";
    $html .= "<html><head>";
    $db = connect_to_db();
    $resultset = run_a_query($db);
    $title = get_title($resultset);
    $html .= "<title>$title</title>";
    $html .= "</head><body>";
    $html .= "<h1>Results</h1>";
    // loop through $resultset 
    // $html .= track info
    // end loop
    $html .= "</body></html>";
    echo $html;
?>
tonethar
  • 2,112
  • 26
  • 33
  • Much thanks for your participation. I this question should not have been here in the first place, I should have been a lillle more careful... I take note of your valuable housekeeping suggestion – varun Kishnani Dec 06 '12 at 14:27
0

You need to set the relevent HTML tags that you're currently missing.

<html> <head> <title>Your title</title> </head> <body> body stuff </body> </html>

qooplmao
  • 17,622
  • 2
  • 44
  • 69