Guestbook Entry Order

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Guestbook Entry Order

Post by Dale »

Hey ;)

I've made a simple guestbook, with learning scripts from around the net :) Heres the index.php which displays the title of all entries that are added. By default it displays the oldest at the top, but how would i make it display the newest entry at the top of the page?

Heres the index.php code:

Code: Select all

<?php include("header.php"); ?>
<tr valign="top">
<td width="45%" align="left"><font face="Verdana" size="1">Title</font></td>
<td width="35%" align="right"><font face="Verdana" size="1">Author</font></td>
<td width="20%" align="right"><font face="Verdana" size="1">Date Entered</font></td>
</tr>
<?
mysql_pconnect("localhost","dale","***");
mysql_select_db("dale");
$result = mysql_query("select * from guestbook");
while($r=mysql_fetch_array($result))
{	
	$id=$r["id"];
	$title=$r["title"];
	$msg=$r["msg"];
	$author=$r["author"];
	$email=$r["email"];
	$date=$r["date"];
	echo "<tr valign="top">
<td width="45%" align="left"><font face="Verdana" size="1"><a href="./view.php?id=$id">$title</a></font></td>
<td width="35%" align="right"><font face="Verdana" size="1"><a href="mailto:$email">$author</a></font></td>
<td width="20%" align="right"><font face="Verdana" size="1">$date</font></td>
</tr>";
}
?>

<?php include("footer.php"); ?>
My coding isnt up to standards, its very messy and i dont have any comments in there at the moment, if you want to modify it for me by sticking comment blocks in then i'd be grateful :D
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

OMG! Why you little pfffft! Use heredoc 8). Heh.

I see you have the column 'id'. So when selecting you can do a:

Code: Select all

SELECT id, title, msg, author, email, date FROM guestbook ORDER BY id DESC
then echo it out as:

Code: Select all

echo <<< ROW
<p>{$r['msg']}
ROW;

// or

echo "<p>{$r['msg']}";
the { } are really useful. And you might also want to stripslashes() and nl2br().

-Nay
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

Erm.. could you like enter that yourself please because im still very new to PHP and im a bit brain dead... sorry :)
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

In your case;

Code: Select all

// change
$result = mysql_query("select * from guestbook");
// to
$result = mysql_query("select * from guestbook order by id desc");
http://www.mysql.com/doc/en/Sorting_rows.html for more info.


What Nay is meaning is that heredoc ( http://php.net/manual/en/language.types ... ax.heredoc ) is another way to print out the data to the screen.
That makes the code abit more readable, but it has nothing to do with the actual issue. Mentioning it so you don't get confused.
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

Thanks
Post Reply