Forum Moderators: coopster

Message Too Old, No Replies

Next previous page

         

adammc

12:38 am on Sep 14, 2004 (gmt 0)

10+ Year Member



Hi,
I want to split my guestbook entries onto different pages (limit 5 entries per page).

I have searched google and couldnt find an easy way to modify my code to accomodate for this.

Can anyone help?


<?
//the host, name, and password for your mysql
mysql_connect("localhost","user","pass");

//select the database
mysql_select_db("epidemic_guestbook");

if($submit)
{
//use the PHP date function for the time

$offset = 3600*10;
$time = gmdate("dS F Y, G:i", time() + $offset);

// inserting it into the shoutbox table which we made in the mysql statements before
$result=MYSQL_QUERY("INSERT INTO shoutbox (id,name,message,time)".
"VALUES ('NULL','$name', '$message','$time')");
}
?>

<?
//returning the last 5 messages
$result = mysql_query("select * from shoutbox order by id desc limit 5");

//the while loop
while($r=mysql_fetch_array($result))
{
//getting each variable from the table
$time=$r["time"];
$id=$r["id"];
$message=$r["message"];
$name=$r["name"];
?>

<b>Posted by:</b>&nbsp;<font color="red"><? echo $name?></font><br>
<b>Date:</b>&nbsp;<? echo $time?><br>
<b>Message:</b><br>
<? echo wordwrap($message, 50, '<br>',1)?><br><br><br>
<? }?>

SkyDog

5:25 am on Sep 14, 2004 (gmt 0)

10+ Year Member



Your going to have to pass the page number ($p) as a query string. Then simply base your sql statement upon that.

define("N",5);
$p=preg_replace("/[^0-9]/","",$_GET['p']);
if(!$p)$p=1;
$sql="select * from shoutbox";
$result=mysql_query($sql);
$max=ceil(mysql_num_rows($result)/N);
if($p>$max)$p=1;
$min=($p-1)*$N;
mysql_free_result($result);
$sql="select * from shoutbox order by id desc LIMIT $min,$N";
// your page or prev next display
if($max>1){
//figure this out yourself
}

SkyDog

5:27 am on Sep 14, 2004 (gmt 0)

10+ Year Member



All instances of $N in the code example should just be N.

adammc

1:09 pm on Sep 15, 2004 (gmt 0)

10+ Year Member



So where exactly in my script do I plave that code, sorry I am still trying to get my head around PHP