Jump to content

Displaying A Set Amount Of Data Per Page


turntabletux

Recommended Posts

Hey guys,I am currently working on a personal website, using only: XHMTL, CSS, PHP & MySQL. I have a simple MySQL database set up in which I will make blog posts. I am still a little new to PHP so I was wondering if there was a simple line of code to only display a certain amount of content per page. And then I can have a "previous entries." button which will display others. I am not going to be posting too many articles, just enough where I don't want them displayed all on one page. Any help would be greatly appreciated, and if I am not being as clear as I could be I will try and explain it a little better. Thanks a ton!-Ant

Link to comment
Share on other sites

:) pagination. The basic idea is to use LIMIT in your database queries to only get a certain result set. For example, if you only want to display 10 results per page, you use the first argument of the LIMIT statement to decide where to start and the second to choose how many pages.Page one's query would look like:"SELECT * FROM `table` LIMIT 0, 10"And for page two:"SELECT * FROM `table` LIMIT 10, 10"You would typically fetch the first of these arguments from the query string:mypage.com?page=1To construct the query with that:"SELECT * FROM `table` LIMIT".$_GET["page"]*10.",10";You get the idea.
Link to comment
Share on other sites

You can probably use something like this:

<?php$offset = (int) $_GET['offset'];if ($offset < 0) {   $offset = 0;}// Items per page$items = 10;// <-- Connect to database here$result = mysql_query ("SELECT * FROM table ORDER BY postdate DESC LIMIT $offset, $items") or die(mysql_error());// <-- Close database here$rownum = mysql_num_rows ($result);$i = 0;// Generate resultswhile ($i < $rownum) {   $title = mysql_result($result,$i,'title');   $text = mysql_result($result,$i,'text');   echo "<br><b>Title: $title<br>Text: $text";   $i++;}?>

And as for page numbers you can do

<a href="blog.php?offset=<?=($offset-$items);?>">PREV</a> || <a href="blog.php?offset=<?=($offset+$items);?>">NEXT</a>

Hope it helps!

Link to comment
Share on other sites

Thanks a ton guys. I am going to try manipulating some of these examples tomorrow. I will post back my results, thanks again!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...