2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by cjohnweb
1

I would love to share with you a nice function I made just for this. But to impliment it you might need to give me some more info - basically just your database schema - table name and column names. The rest is all up to you!

For your project you want to do something like this

general idea of your database schema
ip,date,country,city,region

$paginate = pagination("SELECT * FROM visitors ORDER BY date DESC","main.php?pagination=",$_GET['pagination'],10,true);

The function should handle all of your pagination math and what not, now you just need to use it's output....

<?php

$gv = mysql_query($paginate['sql']);
$cgv = mysql_num_rows($gv);
if($cgv > 0){
while($agv = mysql_fetch_array($gv)){
/*mysql columns ip,date,country,city,region*/

echo "IP: $ip<br />";
echo "Country: $country<br />";
echo "City: $city<br />";
echo "Region: $region<br />";
echo "Date: $date<br />";

}

echo $return['total']." pages of results.<br /><br />";
echo $return['display']."<br /><br />";
echo $return['pages'];

}else{
echo "no results";
}
?>

You should be able to intergrate it into your own project pretty easily. Let me know if you need help - email me john@iluvjohn.com

Don't forget to put the function into your script too:

 <?php                                               
 /*******************************************************************************
 *
 *   Pagination Functions
 *   By John Minton 10/16/2010
 *   Originally for  GardenOfTomorrow.Com 
 *
 *
 *
 *   string pagination( string $sql, string $url, [int $pagination, int $perpage, string $usehtml ] )
 *
 *   string $sql
 *     This can be nearly any SQL "SELECT" statment, such as, "SELECT * 
 *     FROM posts", that can end with a MySQL "LIMIT #,#" statement. The 
 *     pagination function will turn this SQL statement into the proper 
 *     "SELECT * FROM posts LIMIT #1 , #2" used for pagination. 
 *             
 *   string $url
 *     This is the url used in the pagination links, minus the actual 
 *     pagination page #. ie
 *       http://domain.com/somepage.php?pagination=
 *       ?page=home&pagination=
 *       or just "?pagination=" for simple projects!
 *     
 *   int $pagination
 *     This variable carries the page that you are on in your pagination 
 *     script. It will default to 1 if its not numeric, or greater than the 
 *     total # of pages. eg set to $_GET['pagination']
*
 *  int $perpage
 *     This variable specifies how many results you want per page. Defaults 
 *     to 10 if not numeric or left blank.
 *     
 *  string $usehtml
 *     This variable can be set to true or false. If set to false, it will 
 *     return text only. If set to true, or left blank, it will return HTML 
 *     divs around the returned results. Defaults to true.     
 *
 *     
 *  Usage:
 *  
 *  $return = pagination("SELECT * FROM community ORDER BY c_date 
 *   DESC","somepage.php?pagination=",$pagination,10,true);
 *    
 *  echo $return['pages'];
 *  echo $return['display'];
 *   
 *  $gl = mysql_query($return['sql']);
 *   
 *  // Will Return an array of:
 *  $return['sql'] Your original SQL statement, now with proper pagination "LIMIT #,# " appended!
 *  $return['total'] Total number of pages in a <div id='pagination-total'> 
 *  $return['pages'] You pagination links in a <div id='pagination-pages'> 
 *  $return['display'] You pagination links in a <div id='pagination-display'> 
 *       
 *  $paginate = pagination("SELECT * FROM simple_community ORDER BY sc_date DESC","somepage.php?pagination=",$pagination,10,true);
 *  print_r($paginate);
 * 
 *     Array {
 *     [sql] => SELECT * FROM simple_community ORDER BY sc_date DESC LIMIT 0, 10
 *     [total] => <div id="pagination-total">97 Results</div>
 *     [pages] => <div id="pagination-pages">Pages: <a href="?page=community&section=trade&pagination=1">1</a> <a href="?page=community&section=trade&pagination=2">2</a> <a href="?page=community&section=trade&pagination=3">3</a> <a href="?page=community&section=trade&pagination=4">4</a> <a href="?page=community&section=trade&pagination=5">5</a> <a href="?page=community&section=trade&pagination=6">6</a> <a href="?page=community&section=trade&pagination=7">7</a> <a href="?page=community&section=trade&pagination=8">8</a> <a href="?page=community&section=trade&pagination=9">9</a> <b>10</b> </div>
 *     [display] => <div id="pagination-display">Displaying results 91 - 97</div>
 *     }
 *     
 *     
 ******************************************************************************/
function pagination($sql,$pagesurl,$pagination,$perpage = null,$usehtml = null){

if($perpage == null || !is_numeric($perpage)){$perpage = "10";}
if($usehtml == null || $usehtml == true){$usehtml = true;}else{$usehtml = false;}
$gt = mysql_query($sql);
$total = mysql_num_rows($gt);
$pgs = ceil($total/$perpage);
if($pgs < 1){$pgs = 1;}
if(!is_numeric($pagination) || $pagination > $pgs){$pagination = "1";}
for($i = 1;$i<=$pgs;$i++){
if($i == $pagination){
$pages .= "<b>$i</b> ";
}else{
$pages .= "<a href=\"$pagesurl"."$i\">$i</a> ";
}
}
if($usehtml == true){
$pages = "<div id=\"pagination-pages\">Pages: $pages</div>";
}else{
$pages = "Pages: $pages";
}
$nextlimit = $pagination * $perpage - $perpage;
if($usehtml == true){$display = "<div id=\"pagination-display\">";}
$display .= "Displaying results ";
if($pgs == 1){
$display .= $nextlimit; // +1 to offset 0 count
}else{
$display .= $nextlimit+1; // +1 to offset 0 count
}
$display .= " - ";
if($pgs == $pagination){
$display .= $total;
}else{
$display .= $nextlimit+$perpage;
}
$display .= " of $total";
if($usehtml == true){$display .= "</div>";}
if($usehtml == true){$totalresults = "<div id=\"pagination-total\">";}
$totalresults .= "$total Result";
if($total != 1){$totalresults .= "s";}
if($usehtml == true){$totalresults .= "</div>";}
$return['sql'] = $sql." LIMIT $nextlimit, $perpage";
$return['total'] = $totalresults; // 97 Results
$return['pages'] = $pages; // Pages: 1 2 3 <b>4</b> 5, etc
$return['display'] = $display; // Displaying results 31 - 40
return($return);

}
?>

Edited by diafol: apply code tags

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.