Page 1 of 1

Guestbook Entry Order

Posted: Sun Jan 04, 2004 10:54 am
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

Posted: Sun Jan 04, 2004 10:59 am
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

Posted: Sun Jan 04, 2004 11:03 am
by Dale
Erm.. could you like enter that yourself please because im still very new to PHP and im a bit brain dead... sorry :)

Posted: Sun Jan 04, 2004 11:26 am
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.

Posted: Sun Jan 04, 2004 11:57 am
by Dale
Thanks