Limit Entries

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
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Limit Entries

Post by Kriek »

What is an easy way to limit the entries per page of a PHP guestbook without using MySQL?
Currently I have to limit the entries manually (pain in the neck) The entries are added via form.
zacky
Forum Newbie
Posts: 19
Joined: Fri Nov 29, 2002 6:08 am

Post by zacky »

hmm, yea.. I have that problem too..
I selelect * from user :P
to see all users in my forum.. but there are more than 80 users now..
How can I fix it so there is only, hmm.. 30 users? on each page..
So I can fix, Page 1, 2, 3 and so on..
How can i do that? :roll:
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

OK no matter what, your going to need to use MySQL and PHP together to get this to work.

Now first off:

on the page you want the entries to limited place this before your guestbook table.

$table = "DB_Table_Name";
$limit = 5; // How many entires per page.
if ($offset == "all") {
$questlist = mysql_query("Your MySQL code here to get the entries out of the DB and how you want them ordered");
}
else if (!isset($offset) || $offset==0) {
$guestlist = mysql_query("Your MySQL code here to get the entries out of the DB and how you want them ordered LIMIT $limit");
}
else {
$l_offset = $offset*$limit;
$guestlist = mysql_query("Your MySQL code here to get the entries out of the DB and how you want them ordered LIMIT $l_offset,$limit");
}

while ($guestbook = mysql_fetch_array($guestlist)) {
$variable = $guestbook["database_field"];
// Change the above to match your database fields
// Just replicate the format to how many fields you have

?>

Put your html code for the guestbooks appearance putting this <?=$variable ?> (Remember, according to what you have above) whereever you want information to be extracted from your database.

<?php
if ($offset != "all") {
$num_result = mysql_query("SELECT * FROM $table");
$num_p = mysql_num_rows($num_result);
@mysql_free_result($num_result);
$p_offset = $offset-1;
if ($offset>0) echo "<div align='center'><a href=\"yourpage.php?offset=$p_offset\">Previous 5</a></div>";
$n_offset = $offset+1;
?>
<?php
if (($offset*$limit) < $num_p || ($offset*$limit+$limit) < $num_p) echo "<div align='center'><a href=\"yourpage.php?offset=$n_offset\">Next 5</a></div>";
} else {
} ?>


There ya go. That should do it. Any questions regarding this i'll answer.
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post by Kriek »

evilcoder wrote:OK no matter what, your going to need to use MySQL and PHP together to get this to work.
There are several PHP Guestbooks that use flatfiles to store entries instead of MySQL and they still limit the entries per page. It just appears everyone is using a different method for the same function.
Last edited by Kriek on Wed Dec 25, 2002 6:41 pm, edited 2 times in total.
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

ahhh ok. So your guestbook is flatfile. hmmnn. i dont know much about flatfile databases. sorry bout that.
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post by Kriek »

The reason I did not write this Guestbook in PHP/MySQL is because this client does not have access to a database. It would have been so much easier, because I could have wrote it along the same lines as my PHP/MySQL driven shoutbox.
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

ohhh rightio. Sorry i couldnt elp. will look into it for you though.
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post by Kriek »

Thanks for your help =)
Post Reply