Forum Moderators: coopster

Message Too Old, No Replies

problems calling a class

         

steven420

12:21 am on Jan 29, 2007 (gmt 0)

10+ Year Member



Hi, I am having problems with this class:
 <?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.'">&lt; 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 &gt;</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
?>

here is the code on the page:
 <?php
require('pager.php');
$page = new pager("deals", 3);
?>

I would appreciate any help or suggestions.

Thanks,
Steven.

eelixduppy

1:00 pm on Jan 29, 2007 (gmt 0)



What is the problem? Are you getting any error messages displayed in the browser or in your error logs? Clarifying this will help the debugging process.

steven420

3:02 pm on Jan 29, 2007 (gmt 0)

10+ Year Member



I'm not getting any errors. the function works i'm just having problems creating a class with it (I'm still new to classes)

steven420

10:44 pm on Jan 29, 2007 (gmt 0)

10+ Year Member



I fixed the problem. Here is the class in case anyone has a need for it:
this is the connect class:

<?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
?>

this is the pagination 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.'">&lt; 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 &gt;</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
?>

and this is the code for the page:
<?php
require('connect.php');
require('pager.php');
$page = new pager($table_name, $max_results);
echo $page->pagination;
?>