Webpage title using PHP and MySQL?

Hi all,

I have a page where if you visit the example www.site.com/eg.php?id=5, then it will bring you to the page with row 5 details.

However is it possible to put it in the title? I want the title to show the $rows[‘name’] of the database with the id from the link. I did some research and it says to include this <title><?php echo $page_title; ?></title>, however it does not display properly. Any ideas?

How about

 <title><?php echo $rows['name']; ?></title>

If you want to display $rows[‘name’], echoing the value of another variable won’t do you no good, unless in your script you put the value of $rows[‘name’] in that other variable.

Hi, I’ve tried this and it still returns nothing :frowning: thanks for the suggestion though, I’m dumb enough to not think of that. By the way, does using PDO to connect to the database have anything to do with returning the info?

If you use PDO to connect to the database, then you’ll use PDO to retrieve the data.
Please post your code.

Here is what I use to connect:

<?php
$id = $_GET[‘id’];

// Connect to db
try
{
$pdo = new PDO(‘mysql:host=localhost;dbname=eugechin_restaurants’, ‘eugechin’, ‘eugenechin21’);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec(‘SET NAMES “utf8”’);
}
catch (PDOException $e)
{
echo “Unable to connect to the database server!<br>” . $e;
exit();
}
?>

No not that part of the code.
The part where you retrieve the data from the database and display it.

Please post the entire script - all of the code. Cheers.

Here’s the whole code of my php page. Thanks! I took away some unnecessary bits and removed database passwords.

<?php
$id = $_GET['id'];
 
// Connect to db
try
{
  $pdo = new PDO('mysql:host=localhost;dbname=dbname', 'user', 'pw');
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $pdo->exec('SET NAMES "utf8"');
}
catch (PDOException $e)
{
  echo "Unable to connect to the database server!<br>" . $e;
  exit();
}
?>
<!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>fh|<?php echo $row['name']; ?></title>
<link rel="icon" type="image/png" href="favicon.png" />
<link rel="stylesheet" href="style.css" />
<link rel="stylesheet" type="text/css" href="css/resets.css" />
<link rel="stylesheet" type="text/css" href="css/jbslider.css" />
<link rel="stylesheet" type="text/css" href="css/jquery.jscrollpane.css" />
<link rel="stylesheet" type="text/css" href="custom_css.css" />
<script type="text/javascript" src="js/jquery-1.7.min.js"> </script>
<script type="text/javascript" src="js/jquery-ui-1.8.12.min.js"> </script>
<script type="text/javascript" src="js/jquery.jscrollpane.min.js"> </script>
<script type="text/javascript" src="js/jbslider.min.js"> </script>
<script type='text/javascript' src='/js/lib/mootools-core-1.3-full-compat-nc.js'></script>
<script type="text/javascript">
function Toggle(id,close,open,ms){
 var obj=document.getElementById(id),o=Toggle['zxc'+id],to;
 if (!o&&obj){
  Toggle['zxc'+id]=o={
   obj:obj,
   now:0,
   ms:500,
   ud:true
  }
 }
 if (o){
  ms=typeof(ms)=='number'?ms:o.ms;
  obj.style.display='block';
  to=o.ud?open:close;
  clearTimeout(o.dly);
  animate(o,o.now,to,new Date(),ms*Math.abs((to-o.now)/open));
  o.ud=!o.ud;
 }
}

function animate(o,f,t,srt,mS){
 var oop=this,ms=new Date().getTime()-srt,now=(t-f)/mS*ms+f;
 if (isFinite(now)){
  o.now=Math.max(now,0);
  o.obj.style.height=o.now+'px';
 }
 if (ms<mS){
  o.dly=setTimeout(function(){ oop.animate(o,f,t,srt,mS); },10);
 }
 else {
  o.now=t;
  o.obj.style.height=o.now+'px';
  if (t==0){
   o.obj.style.display='none';
  }
 }
}
</script>
<script type="text/javascript">
animatedcollapse.addDiv('jason', 'optional_attribute_string')
//additional addDiv() call...
//additional addDiv() call...
animatedcollapse.init()
</script>
<link href='http://fonts.googleapis.com/css?family=PT+Sans' rel='stylesheet' type='text/css'>
</head>

<body>
<div id="contain">
<div id="header">
<div id="hwrapper">
		<a href="/index.php" id="logo"><img src="flogo.png" alt="Logo" width="460px" height="140px"/></a>
  </div>
</div>

<div id="container">
	<div id="line">
</div>
<div id="navbar">
    <div id="nav">
    	<ul>
        	<li><a href="index.php">Home</a></li>
            <li><a href="list.php">Restaurant List</a></li>
            <li><a href="submit.php">Submit a Restaurant</a></li>
            <li><a href="about.php">About</a></li>
            <li><a href="#"><div class="button open-button" onMouseUp="Toggle('page-split-wrap',0,250,500);"><span style="font-style:italic; font-size:19px;">Restaurant Search</span></div></a>

</li>
        </ul>
    </div>
 
    <div id="body">
     <div id="page-split-wrap">
  <div class="page-split-head"> </div>
  <div class="page-split-content">
    <p class="inside">
<form method="get" action="/search1/search.php">
<div>

  <input type=  "text" name=     "q" value="Search"  class="inputtext" onFocus="this.value=''"/>
  <input type="hidden" name=   "cat" value=""  class="inputtext"/>
  <input type="hidden" name=  "time" value="0" class="inputtext"/>
  <input type="hidden" name="letter" value=""  class="inputtext"/>
  <input type="hidden" name="offset" value="0" class="inputtext"/>
  <input type="hidden" name=  "type" value="quick"  class="inputtext"/>
  <br />
  <br />
  <div id="submitsearch">
  <input type="submit" style="font-size:16px;" value="Search" class="sbutton"/>
</div>
</div>
</form>

</p>
  </div>
 </div>
    <div id="wrapperb"> 
    <?php
// The query
$id = (int) $_GET['id'];
$sql= $pdo->prepare('SELECT * FROM review WHERE id = :id');
$sql->execute(array(':id' => $id));
if ($sql->rowCount() === 0)
{
	echo '<br>Sorry, but the id did not match anything in the database.
}
else
{
foreach ($sql as $row) {
  echo '<div id="infobar"><span class="restaurantname"><div id="namealign">'.$row['name'].'</div></span><div id="inforight">'.$row['cuisine'].'<br><span class="address">'.$row['address'].'<br>'.$row['phone'].'</div></div><br>'
  
</div>
<div id="footer">
<div id="wrapperb">
		<div id="footerlinks">
        <img src="about.png" width="220" height="207" />
        <img src="contact.png" width="220" height="207" />
        <img src="terms.png" width="220" height="207" />
        <img src="holder.png" width="220" height="207" />
        </div>
    	                <?php include 'footer.php'; ?>

        
        <img src="http://hitwebcounter.com/counter/counter.php?page=4612375&style=0007&nbdigits=5&type=page&initCount=0" title="" Alt=""   border="0" >
</body>
</html>

I’m usually fetch all data from mysql in the top of my script so i can done with dynamic title based on selected page.

but, based on your script above i just see you fetch data from mysql in middle of html tag so

<title>fh|<?php echo $row['name']; ?></title>

would not work as well : undefined index: name?

granted I just gave the code a cursory glance, but I dont see where you sent a query to $pdo (BEFORE) you use it output the title. Worse still there is no iteration ( so there is actually no $row, and thus no $row[‘name’], from the already EMPTY $pdo.

a lot more strategic thinking needs to be done here. I mean if your query resulted in 20 articles, all which will display on a single page WHAT would be your title? The headline of the first article? the category where it comes from? part of the query string???

once you decide that you can do your query even before your <html> tag and use the retrieved information to build the title string accordingly. then you just output it as suggested above

First of all, you need to store $row[‘name’] in a variable and put that in your controller. Also make sure that it is BELOW your foreach. Currently your foreach statement is at the very bottom of your script. Therefore your varaible needs to be below that or else you will get an undefined variable.


<?php

$retrieverow = $row['name'];


Once you do this, then you can pass that varaible to your html title page like this:


<title><?php echo htmlspecialchars($retrieverow, ENT_QUOTES, 'utf-8'); ?></title>