ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

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

user-image
تم إضافة السؤال من قبل مستخدم محذوف‎
تاريخ النشر: 2013/04/06
Zaid Rabab'a
من قبل 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
من قبل 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.

المزيد من الأسئلة المماثلة