Forum Moderators: coopster
<?php
class pager extends connect
{
$var1=new connect();
$var1->link2($var1->db);
function pager($table_name, $max_results)
{
/* Set current, prev and next page */
$page = (!isset($_GET['page']))? 1 : $_GET['page'];
if (!$page)
{
$page = 1;
}
$prev = ($page - 1);
$next = ($page + 1);
/* Max results per page */
if (!$max_results)
{
$max_results = 15;
}
/* Calculate the offset */
$from = (($page * $max_results) - $max_results);
/* Query the db for total results. You need to edit the sql to fit your needs */
$result = mysql_query("SELECT * FROM $table_name");
$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $max_results);
$pagination = '';
/* Create a PREV link if there is one */
if($page > 1)
{
$pagination .= '<a href="'.$_SERVER['PHP_SELF'].'?page='.$prev.'">< Previous</a> ';
}
/* Loop through the total pages */
for($i = 1; $i <= $total_pages; $i++)
{
if(($page) == $i)
{
$pagination .= $i;
}
else
{
$pagination .= '<a href="'.$_SERVER['PHP_SELF'].'?page='.$i.'"> '.$i.' </a>';
}
}
/* Print NEXT link if there is one */
if($page < $total_pages)
{
$pagination .= '<a href="'.$_SERVER['PHP_SELF'].'?page='.$next.'"> Next ></a>';
}
// Now we have our pagination links in a variable($pagination) ready to print to the page. I pu it in a variable because you may want to show them at the top and bottom of the page */
/* Below is how you query the db for ONLY the results for the current page */
$result=mysql_query("SELECT * FROM $table_name LIMIT $from, $max_results");
while ($i = mysql_fetch_array($result))
{
echo $i[0].'<br>';
}
echo $pagination;
}// end function pager
}// end class pager
?> <?php
require('pager.php');
$page = new pager("deals", 3);
?>
Thanks,
Steven.
<?php
class connect
{
var $conn;
var $select_db;
function connect($user, $pass, $data_base, $host)
{
$this->conn = mysql_connect($host, $user, $pass);
if(!$this->conn)
{
echo 'could not connect';
}
$this->select_db = mysql_select_db($data_base) or die('failed: '.mysql_error());
if (!$this->select_db)
{
echo 'could not select db';
}
}//end function
}// end class
?>
<?php
class pager extends connect
{
var $page;
var $prev;
var $next;
var $from;
var $result;
var $total_pages;
var $total_results;
var $pagination;
var $connect1;
var $close;
var $i;
function pager($table_name, $max_results)
{
$this->connect1 = new connect("user", "pass", "db", "host");
/* Set current, prev and next page */
$this->page = (!isset($_GET['page']))? 1 : $_GET['page'];
if (!$this->page)
{
$this->page = 1;
}
$this->prev = ($this->page - 1);
$this->next = ($this->page + 1);
/* Max results per page */
if (!$max_results)
{
$max_results = 15;
}
/* Calculate the offset */
$this->from = (($this->page * $max_results) - $max_results);
/* Query the db for total results. You need to edit the sql to fit your needs */
$this->result = mysql_query("SELECT * FROM $table_name");
$this->total_results = mysql_num_rows($this->result);
$this->total_pages = ceil($this->total_results / $max_results);
$this->pagination = '';
/* Create a PREV link if there is one */
if($this->page > 1)
{
$this->pagination .= '<a href="'.$_SERVER['PHP_SELF'].'?page='.$this->prev.'">< Previous</a> ';
}
/* Loop through the total pages */
for($this->i = 1; $this->i <= $this->total_pages; $this->i++)
{
if(($this->page) == $this->i)
{
$this->pagination .= $this->i;
}
else
{
$this->pagination .= '<a href="'.$_SERVER['PHP_SELF'].'?page='.$this->i.'"> '.$this->i.' </a>';
}
}
/* Print NEXT link if there is one */
if($this->page < $this->total_pages)
{
$this->pagination .= '<a href="'.$_SERVER['PHP_SELF'].'?page='.$this->next.'"> Next ></a>';
}
$this->result=mysql_query("SELECT * FROM $table_name LIMIT $this->from, $max_results");
while ($this->i = mysql_fetch_array($this->result))
{
echo $this->i[0].'<br>';
}
return $this->pagination;
$this->connect1->close = mysql_close($this->connect1->conn);
if (!$this->connect1->close)
{
echo "can't close";
}
}// end function pager
}// end class pager
?>
<?php
require('connect.php');
require('pager.php');
$page = new pager($table_name, $max_results);
echo $page->pagination;
?>