Jump to content

How can I echo blog article's Headline be the page "Title"; I tried this but


mac007

Recommended Posts

Hello, all;

 

I am practising on this blog setup, and I am trying to echo each blog'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 single post's id is passed with $post variable, like "?b=101"; in page body then I have another if statement that again, if $post variable is present, it will either show only first paragraph of each post, or show complete post article, with paragraph breaks.

 

This all seems to be working pretty well, but then when I tried to echo a blog'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 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 script? I guess there must be something that makes sense logically...

 

well, appreciate any help and thanks in advance!

 

 

<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");

} else {

$result = mysql_query ("SELECT * from blog WHERE blog_id = $post");

}

 

 

?>

 

<!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 if (!isset($post)) {echo "Welcome to my Blog!";} else { echo single blog's Headline;} ?></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);

?>

 

 

</CODE>

You don't need to repeat the while loop

 

You said (my changes in bold)....

 

$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");

$titlerow = mysql_fetch_object($result);

$page_title = $titlerow->blog_title;

}

...

 

<title><?php if (!isset($post)) {echo "Welcome to my Blog!";} else { echo single blog's Headline;} ?>

<?php echo $page_title; ?>

</title>

 

I've done something similar using this method.

Hey, Indy:

 

thanks much for your input... I tried your changes, but it's doing same thing like was doing to me before, it does echo blog's headline as page's Title but then it prevents the blog entry from showing in the body section... maybe I am not setting this part correctly either... I probably should condense the while loop in the "head" section too to avoid the conflict??

 

Thanks!

Alright, lemme see if I've got this right...

 

You're looking for a page with "Welcome to my Blog!" as the page title and a list of post titles with a short blurb of the content of that post when no post is selected.  When one is selected, you need the content of that post with the post's title as the page title.  Right?

 

If that's the case, then you can dump the 2nd while loop and add the values to what I showed you before.

 

Something like this:

if (not-a-post){

      page-title = "Welcome to my blog"

}else{

      single row call (no need for while())

      page-title = row->title

      id = row-id

      body = row body}

 

then down below...

 

if(not-a-post){

    while-loop for some number X posts{

        echo post titles & blurbs

    }

}else{

        echo data retrieved above in whatever format you want for a single blog post

}

 

Off the top of my head, that should work.

 

edit: I put your code into an editor and modified the db stuff to pull from a news db here.  It all seemed to work just fine....

<?php 
// this line changed for my db connection file
include_once ('engweb_conn.php'); ?>

<?php
//mysql_select_db ("database_name",$con); - commented out due to my db connect file above
   
$post = $_GET['b'];
   if (!isset($post)) {
$result = mysql_query ("SELECT * from news ORDER BY id DESC");
} else {
$result = mysql_query ("SELECT * from news WHERE id = $post");
}


?>

<!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 if (!isset($post)) {echo "Welcome to my Blog!";} else { echo "headline";} ?></title>
<link href="styles.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="wrapper">
  <div id="header">
    
  </div>
  <div id="leftColumn">Content for  id "leftColumn" Goes Here </div>
  <div id="main">
  <table><tr><td>

  <?php
//changed only field names here.
   if (!isset($post)) {
  while($row = mysql_fetch_array ($result)) {
   echo "<h1>" . $row['id'] . "-" . "<a href='?b=" . $row['id'] . "'>" . $row['title'] . "</a> </h1><br>" .
   substr ($row['newstext'],0,strpos($row['newstext'], "\n")). "<br><br>";
   }
   } else {
     while($row = mysql_fetch_array ($result)) {
   echo "<h1>" . $row['id'] . "-" . "<a href='?b=" . $row['id'] . "'>" . $row['title'] . "</a> </h1><br>" .  nl2br ($row['newstext']) . "<br><br>";
   }
   }
   
?>
  </td></tr></table>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.