- Code: Select all
<?php
// This includes your server information. In this sample I created a function
// called open_cnx($cnx) to perform the connexion with MySQL DB
require("lib/inc/inc.php");
open_cnx($cnx);
// declare the DB table name and id
$tab = 'reguser';
$ids = 'id_reg';
// check if number of pages if set on the address bar
if(isset($_GET['pg'])){
$pg = $_GET['pg'];
} else {
// if is not set, set it to 1
$pg = 1;
}
// get number of rows from database -> Ps: @ tells mysql not to report errors! You can remove it to show errors.
$sql = @mysql_query("SELECT $ids FROM $tab");
$row = @mysql_num_rows($sql);
if($row == 0){
// if no result found, display result message
echo 'No results were found';
} else {
$res = row;
// if there are results to show, set number of results per page
$pageResults = 10; // You can change this value to set the amount of results displayed per page
$pageNumber = ceil($row/$pageResults);
$pg = (int)$pg;
if($pg > $pageNumber){ $pg = $pageNumber; }
if($pg < 1){ $pg = 1; }
$limit = 'LIMIT ' .($pg - 1) * $pageResults .',' .$pageResults;
$sql = @mysql_query("SELECT * FROM $tab $limit ;");
/**/### PUT THIS WHERE YOU WANT TO SHOW THE PAGE'S COUNTER #####//
/**/# if the result its just one page ########################//
/**/ if($pageNumber == '1'){
/**/ echo '1';
/**/############################################################//
/**/ }
/**/ else {
/**/ for($pg=1;$pg <= $pageNumber ;$pg++){
/**/# if more than one page, list with link ###################//
/**/ echo '<a href="'.$_SERVER['PHP_SELF'].'?pg='.$pg.'">'.$pg.'</a>'.' | ';
/**/############################################################//
/**/ }
/**/ }
/**/##### HERE ENDS THE PAGE'S COUNTER #######################//
/**/############################################################//
/**/### PUT THIS WHERE YOU WANT TO SHOW YOUR RESULTS ##########//
/**/ $q = @mysql_query("SELECT * FROM $tab ORDER BY $ids ASC $limit");
/**/### define colors for rows on fetch #######################
/**/ $colour_odd = "#F7F7F7";
/**/ $colour_even = "#FFFFFF";
/**/ $i = 0; //To keep track of row number
/**/##################################################################
/**/ while($r = @mysql_fetch_assoc($q)){
/**/ $row_color = (($i % 2) == 0) ? $colour_even : $colour_odd;
/**/######### use on TR tag: <tr bgcolor="' . $row_color . '"> ######
/**/######### content to display in fetch ###########################
/**/ echo '» [ '.$r['id_reg'].' ]'.$r['name'].' '.$r['lastname'].'<br>';
/**/#################################################################
/**/ $i++;
/**/ }
/**/############################################################//
/**/##### FETCH ENDS HERE ####################################//
/**/############################################################//
} // end else
// don't forget to close the mysql connexion after you're done!!!
close_cnx($cnx);
?>
The first page of the code divides the results into 1 or more pages. The secound part displays the page's links [ 1 | 2 | 3 | 4 | 5 ] and should be placed anywhere under the first part. Ex.: Put the first part before the open tag od < html > then place the secound part wherever you need. The last part should be placed where you want to list the results. The close connexion should be the last line of your code.
If you're enable to understand and use this code will be easy to improve and modify and even style the output.
Thanks for reading and please give your optinion on the poll!