ID: 502
Viewed: 6236
Added: Apr 01, 2003
Version: 1
Snippet uploaded by: Quatro
Written By: SyncMaster
Demo: Sorry, no demo
Thank you for your vote. Please wait...
It appears you already voted for this snippet
It appears your vote value was empty
A easy script that enables you to show # records/page from a mysql db. with next/ prev buttons, and 1 2 3 4 5 6 page(s) etc.
<!---Head--->
///////// EDIT VALUES MATCHING YOUR DATABASE'S ONE
$db_host = ""; // This is the name of your database host (usually localhost)
$db_user = ""; // This is the name of the database user you are going to use
$db_pass = ""; // This is the password for the database
$db_name = ""; // This is the name of the database you are going to use
////////// SET THIS TO THE AMOUNT OF RECORDS DISPLAYED PER PAGE
$result_per_page = 10; //// amount of results per page
$table_name = 'test_table'; /// TABLE FROM WHERE YOU WILL BE QUERING INFO
////////////////// END EDITING ///////////////////////////
<!---Body--->
mysql_connect($db_host, $db_user, $db_pass) || die ("Cannot cannot to database server."); /// connecting to db
mysql_select_db($db_name) || die ("Cannot cannot to the database."); ///selecting db
$result = mysql_query("SELECT * FROM $table_name"); ///// selecting globally from table
$totalrows = mysql_num_rows($result); ///// counting total rows in table
if(empty($page)) { $page = 0; } //// if there's no page set, set offset to 0
$prevpage = ($page-1); $nextpage= ($page+1); /// calculating next and previous page
echo "Page(s): "; /// layout
if (!(($page+1) == 1)){ echo "<a href='$php_self?page=$prevpage'>Previous</a> "; }
$totalpages = ceil($totalrows/$result_per_page); ////// checking total pages needed
$i = 1; ///
while($i<=$totalpages){
$newi = $i-1;
if($newi == $page){
echo "<i>$i</i> "; /// showing amount of pages
} else {
echo "<a href='test.php?page=$newi'>$i</a> "; /// showing amount of pages
}
$i++;
}
if (!(($page+1) == $totalpages)){ echo "<a href='$php_self?page=$nextpage'>Next</a><br>"; }
$start = ($page*$result_per_page); /// calculating start row
$end = ($start+$result_per_page-1); //// calculating end row
echo "<br><br>$start - $end<br><br>";
//////////// PART THAT NEEDS TO BE EDITTED TO YOUR NEEDS (BASIC PHP,MYSQL KNOWLEDGE REQUIRED) ///////////////
$result = mysql_query("SELECT * FROM test_table LIMIT $start, $result_per_page"); //// selecting from table for display
while($row = mysql_fetch_array($result)){
$item = $row["test_item"]; //// edit if needed
echo "$item<br>"; //// echoing the records that are in the table
}
//////////////////////////////////// END EDITING ///////////////////////////////////////
No Comments to show
Please completely fill out the form below if you want to review this snippet. All reviews are subject to validation.
Adding your comment. Please wait...
Thanks for adding your comment!. After further review it will be added.
There was a problem adding your comment. Please try again.
Please complete all the fields in the form before sending.