Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

How I do page my database results into multi-pages? I need PHP code

user-image
Question added by Deleted user
Date Posted: 2013/04/06
Zaid Rabab'a
by Zaid Rabab'a , Technical Team Lead , ESKADENIA Software

In order to do this you also need to use the offset value in your SQL Statement, so it would be SELECT * FROM users LIMIT $offset, $perpage Example: SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15 Then to get the links to put on the bottom of your page you would want to get a count of the total data, divide the total by the per page value to figure out how many pages you are going to have. Then set your offset value based on what page the user clicked. Hope that helps! $result = mysql_query("SELECT * FROM users" ); //Note if you have a very large table you probably want to get the count instead of selecting all of the data... $nrResults = mysql_num_rows( $result ); if( $_GET['page'] ) { $page = $_GET['page'] } else { $page = 1; } $per_page = 2; $offset = ($page - 1) * $per_page; //So that page 1 starts at 0, page 2 starts at 2 etc. $result = mysql_query("SELECT * FROM users LIMIT $offset,$per_page"); while( $line = mysql_fetch_array( $result ) ) { //Do whatever you want with each row in here }

Mohamed Magdy
by Mohamed Magdy , System Administrator & Owner , ModServ, LLC.

Instead of mysql_fetch_array() use mysql_fetch_assoc(), it's faster :) Don't forget to secure $_GET.

Deleted user
by Deleted user

Thank you Zaid

More Questions Like This