| | |
How can I echo blog-article's headline to be the page "Title"; I tried this but didnt
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Mar 2007
Posts: 63
Reputation:
Solved Threads: 0
How can I echo blog-article's headline to be the page "Title"; I tried this but didnt
0
#1 Oct 27th, 2008
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!
<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");
$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);
?>
</CODE>
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!
<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");
$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);
?>
</CODE>
Last edited by websurfer; Oct 27th, 2008 at 5:42 pm.
Re: How can I echo blog-article's headline to be the page "Title"; I tried this but didnt
0
#2 Oct 27th, 2008
$row will be empty with the way you are doing this, you will need at a minimum:
This way the $row will be set and can be used. This will need to be done for all queries.
Also,
php Syntax (Toggle Plain Text)
$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: php Syntax (Toggle Plain Text)
if (is_numeric($post)) { // Code if is numeric value } else { // Tell them off for entering a non-numeric value. }
AJAX is not a programming language, scripting language or any other sort of language.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
•
•
Join Date: Mar 2007
Posts: 63
Reputation:
Solved Threads: 0
Re: How can I echo blog-article's headline to be the page "Title"; I tried this but didnt
0
#3 Oct 27th, 2008
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:
<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);
?>
</CODE>
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:
<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);
?>
</CODE>
Last edited by websurfer; Oct 27th, 2008 at 10:04 pm.
Re: How can I echo blog-article's headline to be the page "Title"; I tried this but didnt
0
#4 Oct 27th, 2008
Try removing the $row line from the top if there is no URL variable, and add the while into the body. for example:
Head:
And Body:
change
To:
Head:
php Syntax (Toggle Plain Text)
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
php Syntax (Toggle Plain Text)
} 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:
php Syntax (Toggle Plain Text)
} 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>"; } }
AJAX is not a programming language, scripting language or any other sort of language.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
•
•
Join Date: Mar 2007
Posts: 63
Reputation:
Solved Threads: 0
Re: How can I echo blog-article's headline to be the page "Title"; I tried this but didnt
0
#5 Oct 27th, 2008
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...
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...
Last edited by websurfer; Oct 27th, 2008 at 10:28 pm.
Re: How can I echo blog-article's headline to be the page "Title"; I tried this but didnt
0
#6 Oct 27th, 2008
![]() |
Other Threads in the PHP Forum
- Previous Thread: Please Help Me Out !!!
- Next Thread: Can you make this word wrap?
| Thread Tools | Search this Thread |
.htaccess ajax apache api array beginner binary broken buttons cakephp checkbox class cms code cron curl database date directory display dynamic ebooks echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla limit link login loop mail mediawiki menu mlm mod_rewrite multiple mysql number oop paypal pdf php phpincludeissue phpmyadmin problem query radio random recursion regex remote script search server sessions sms soap source sp space speed sql subdomain syntax system table tag tutorial update upload url validation validator variable vbulletin video web webdesign websphere white xml youtube





