...

View Full Version : Display more than one post



Millenia
08-11-2008, 09:28 PM
I am making a blog for me in PHP. Although the actual part that displays the posts, only displays 1. I'm a newbie to PHP and I don't know quite what's wrong. The script pulls the posts out of a database. (No admin CP for me!)

Can anyone help me, I would like it to display 6 posts.


<!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" />
<meta name="Description" content=" " />
<meta name="Keywords" content=" " />

<title>My Blog</title>
<link href="style.css" rel="stylesheet" type="text/css" />

</head>

<body>


<?php
define('IN_BLOG', true);
require_once "config.php";

$sql = mysql_connect( $mysqlHost, $mysqlUsername, $mysqlPassword) or die(mysql_error());
mysql_select_db($mysqlDatabase, $sql) or die(mysql_error());
$row = mysql_query("SELECT * FROM blog_posts") or die(mysql_error());

while ( $row = mysql_fetch_array( $row ) )
{
echo "<h2>" . $row['title'] . "</h2>" . "<p>" . $row['content'] . "</p><p><small>Posted by - <b>" .$row['author']. "</b> - on the date -<b>" . substr($row['timestamp'], -19, 11) .".</b></p></small>";
}
?>
</body>
</html>

Thanks

(If you need any more scripts from the blog, then let me know.)

Brandoe85
08-11-2008, 09:41 PM
You're assigning $row to the mysql result and then again when you fetch. Use a different variable for the result:


$result = mysql_query("SELECT * FROM blog_posts") or die(mysql_error());
while ( $row = mysql_fetch_array( $result ) )
Take a look at mysql's LIMIT to show only 6 records:
http://dev.mysql.com/doc/refman/5.0/en/select.html

Good luck;

Millenia
08-11-2008, 09:49 PM
You're assigning $row to the mysql result and then again when you fetch. Use a different variable for the result:


$result = mysql_query("SELECT * FROM blog_posts") or die(mysql_error());
while ( $row = mysql_fetch_array( $result ) )
Take a look at mysql's LIMIT to show only 6 records:
http://dev.mysql.com/doc/refman/5.0/en/select.html

Good luck;

Thanks, can you answer me another?

The blog posts are displayed from the oldest at the top, to the newest at the bottom, is there a way to change that? So it's like a normal blog. The latest added posts are displayed at the top.

Brandoe85
08-11-2008, 09:53 PM
Sure- look at ORDER BY in the same link. You probably have a blog date, order by that:
select fields from table order by dateField desc limit 6

Good luck;

Millenia
08-11-2008, 09:56 PM
Sure- look at ORDER BY in the same link. You probably have a blog date, order by that:
select fields from table order by dateField desc limit 6

Good luck;

I am a SQL noob aswell :rolleyes:

If you don't mind answering,
How can I do that?


$result = mysql_query("SELECT * FROM blog_posts ORDER BY timestamp;
") or die(mysql_error());

This didn't work

Brandoe85
08-11-2008, 09:58 PM
I'll go with your timestamp field (looking at your original post), so run the query like:

Sure- look at ORDER BY in the same link. You probably have a blog date, order by that:
select * from table order by timestamp desc limit 6

Good luck;

Millenia
08-11-2008, 10:01 PM
I'll go with your timestamp field (looking at your original post), so run the query like:

Thank you so much,
it works perfectly!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum