Hello, all;

I am practising on this blog setup, and I am trying to echo each blog entry's headline appear as the page's Title tag... somehow the way I have it doesnt do it. So that you have an idea of what I am doing, I have an "if" statement in head section that will select "all" records, unless a unique post's id is passed with $post variable, like "?b=101"; in page body then I have another "if" statement that again does "if $post variable is NOT present, it will either show all posts with just first paragraph teaser, else show complete individual article, with paragraph breaks"...

This all seems to be working pretty well, but then when I tried to echo an entry's headline as the page's "Title" tag, I figured I needed to do another if statement with another "while" loop, wich did work and would echo each's individual blog's headline as page "title", but then it prevented the actual blog-entry itself from showing in body section...

I just left it simply as you see in code below for now. I am thinking maybe I can't repeat the "while($row = mysql_fetch_array ($result))" command line twice within same page? or maybe it doesnt pick up the title cause it's calling the "Title" tag variable before the while loop in the body?? I guess there must be something that makes sense logically...

well, appreciate any help and thanks in advance!

<?php include_once ('definitions.php'); ?>


<?php
mysql_select_db ("database_name",$con);

$post = $_GET['b'];
   if (!isset($post)) {
$result = mysql_query ("SELECT * from blog ORDER BY blog_id DESC");
$page_title = "Welcome to my Blog!";
} else {
$result = mysql_query ("SELECT * from blog WHERE blog_id = $post");
$page_title = $row['blog_title'];
}


?>

<!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><?php echo $page_title; ?></title>
<link href="styles.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="wrapper">
  <div id="header">
        <?php include('header.php'); ?>
  </div>
  <div id="leftColumn">Content for  id "leftColumn" Goes Here </div>
  <div id="main">
  <table><tr><td>



  <?php

   if (!isset($post)) {
  while($row = mysql_fetch_array ($result)) {
   echo "<h1>" . $row['blog_id'] . "-" . "<a href='?b=" . $row['blog_id'] . "'>" . $row['blog_title'] . "</a> </h1><br>" .
   substr ($row['blog_body'],0,strpos($row['blog_body'], "\n")). "<br><br>";
   }
   } else {
     while($row = mysql_fetch_array ($result)) {
   echo "<h1>" . $row['blog_id'] . "-" . "<a href='?b=" . $row['blog_id'] . "'>" . $row['blog_title'] . "</a> </h1><br>" .  nl2br ($row['blog_body']) . "<br><br>";

   }
   }


?>


  </td></tr></table>

  </div>
</div>
</body>
</html>
<?php
mysql_close($con);
?>

Recommended Answers

All 5 Replies

$row will be empty with the way you are doing this, you will need at a minimum:

$result = mysql_query ("SELECT * from blog ORDER BY blog_id DESC")or die(mysql_error());
$row = mysql_fetch_assoc ( $result )or die(mysql_error());

This way the $row will be set and can be used. This will need to be done for all queries.

Also, $post = $_GET['b']; is NOT good practice, anyone can put anything into the query string and it will be included in the sql query. Do some validation on the input, for example:

if (is_numeric($post)) {
  // Code if is numeric value
} else {
  // Tell them off for entering a non-numeric value.
}

Hi, Xan:

thanks a lot for your reply... I couldnt exaclty make out what you meant as far as placing the two lines of code you mention (the $result and $row lines); I placed them in the "HEAD" section as I think you meant. See in my code here again how I placed them, and it did display stuff closer to how I need it to be.. It does show each particular post's headline as the page "Title" tag, and show's respective post in body section, BUT when I re-load INDEX page normally, (with no url blog-ID string), it only shows the first post in the blog, and not the rest of them... I am sure is cause I took the "while" statements out of the Body section as I had it in my earlier code version.. but I just couldnt get it to work anywehere I placed it...

I see your point about the url-variable validation, too.

Appreciate your help! :)

Here's my revised code:

<?php include_once ('definitions.php'); ?>


<?php
mysql_select_db ("database_name",$con);

$post = $_GET['b'];
   if (!isset($post)) {
$result = mysql_query ("SELECT * from blog ORDER BY blog_id DESC") or die(mysql_error());
$row = mysql_fetch_assoc ( $result )or die(mysql_error());
$page_title = "Welcome to my Blog!";
} else {
$result = mysql_query ("SELECT * from blog WHERE blog_id = $post") or die(mysql_error());
$row = mysql_fetch_assoc ( $result )or die(mysql_error());
$page_title = $row['blog_title'];
}


?>

<!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><?php echo $page_title; ?></title>
<link href="styles.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="wrapper">
  <div id="header">
        <?php include('header.php'); ?>
  </div>
  <div id="leftColumn">Content for  id "leftColumn" Goes Here </div>
  <div id="main">
  <table><tr><td>



  <?php

   if (!isset($post)) {

   echo "<h1>" . $row['blog_id'] . "-" . "<a href='?b=" . $row['blog_id'] . "'>" . $row['blog_title'] . "</a> </h1><br>" .
   substr ($row['blog_body'],0,strpos($row['blog_body'], "\n")). "<br><br>";

   } else {

   echo "<h1>" . $row['blog_id'] . "-" . "<a href='?b=" . $row['blog_id'] . "'>" . $row['blog_title'] . "</a> </h1><br>" .  nl2br ($row['blog_body']) . "<br><br>";


   }


?>


  </td></tr></table>

  </div>
</div>
</body>
</html>
<?php
mysql_close($con);
?>

Try removing the $row line from the top if there is no URL variable, and add the while into the body. for example:
Head:

if (!isset($post)) {
$result = mysql_query ("SELECT * from blog ORDER BY blog_id DESC") or die(mysql_error());
$page_title = "Welcome to my Blog!";
} else {

And Body:
change

} else {
echo "<h1>" . $row['blog_id'] . "-" . "<a href='?b=" . $row['blog_id'] . "'>" . $row['blog_title'] . "</a> </h1><br>" . nl2br ($row['blog_body']) . "<br><br>";

}

To:

} else {
while ( $row = mysql_fetch_assoc ( $result ) ) {
  echo "<h1>" . $row['blog_id'] . "-" . "<a href='?b=" . $row['blog_id'] . "'>" . 
  $row['blog_title'] . "</a> </h1><br>" . nl2br ($row['blog_body']) . "<br><br>";
  }
}

IT WORKED!!!!!!

You are great!... hey, let me ask a quick question since I am a newbie at this PHP stuff: besides the obvious mis-placement of some parts of the code, the main problem in all of this is that I had not "set" the value of the $row before I had echoed it in the "title" tag ha??

Also, I guess since the URL variable calls for the specific post ID, there is no need for a "while statement when requesting a specific post

Gonna check over your code and study it carefully...

Appreciate your help!
Thanks...

Your assumptions are spot on to both of those :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.