View Full Version : Problem initializing a class

01-06-2012, 07:53 PM
Hi Guys,

For some strange reason my class doesn't seem to want to initialize. This exact script works on my website currently, but is giving me a problem on my local development pc. Not quite sure what's wrong. This is a custom wordpress theme so its split up funky. I keep getting this error Fatal error: Call to a member function display_pages() on a non-object.

This is inside my page template.

include_once( LP_CLASSES_DIR . '/pagination-class.php');

/* Get the records */
$drawing_count = HistData_Model::total_drawing_dates();

/* Define pagination */
$pages = new Pagination;
$pages->items_total = $drawing_count[0]['count(*)'];
$pages->mid_range = 7;

echo $pages->display_pages();
echo $pages->display_items_per_page();
echo $pages->display_jump_menu();

foreach($drawings_array as $new_drawing):
echo $new_drawing; //This comes from further up and populates fine when you print array out.
echo "<br />";
echo $pages->display_pages();

this is my pagination class


class Pagination{
var $items_per_page;
var $items_total;
var $current_page;
var $num_pages;
var $mid_range;
var $low;
var $high;
var $limit;
var $return;
var $default_ipp = 10;
var $querystring;

function Pagination(){
$this->current_page = 1;
$this->mid_range = 7;
$this->items_per_page = (!empty($_GET['ipp'])) ? $_GET['ipp']:$this->default_ipp;

function paginate(){
if( isset( $_GET['ipp'] ) ): $selected_ipp = $_GET['ipp']; else: $selected_ipp = $this->default_ipp; endif;
if( isset( $_GET['page'] ) ): $selected_page = $_GET['page']; else: $selected_page = 1; endif;
$mod = explode("?", $_SERVER['REQUEST_URI']);
$php_self = $mod[0];
if($selected_ipp == 'All'): //changed from $_GET
$this->num_pages = ceil($this->items_total/$this->default_ipp);
$this->items_per_page = $this->default_ipp;
if(!is_numeric($this->items_per_page) OR $this->items_per_page <= 0) $this->items_per_page = $this->default_ipp;
$this->num_pages = ceil($this->items_total/$this->items_per_page);
$this->current_page = (int) $selected_page; // must be numeric > 0
if($this->current_page < 1 Or !is_numeric($this->current_page)) $this->current_page = 1;
if($this->current_page > $this->num_pages) $this->current_page = $this->num_pages;
$prev_page = $this->current_page-1;
$next_page = $this->current_page+1;

$args = explode("&",$_SERVER['QUERY_STRING']);
foreach($args as $arg):
$keyval = explode("=",$arg);
if($keyval[0] != "page" And $keyval[0] != "ipp") $this->querystring .= "&" . $arg;

foreach($_POST as $key=>$val):
if($key != "page" And $key != "ipp") $this->querystring .= "&$key=$val";

if($this->num_pages > 10):
$this->return = ($this->current_page != 1 And $this->items_total >= 10) ? "<a class=\"paginate\" href=\"".$php_self."?page=$prev_page&ipp=$this->items_per_page$this->querystring\">&laquo; Previous</a> ":"<span class=\"inactive\" href=\"#\">&laquo; Previous</span> ";

$this->start_range = $this->current_page - floor($this->mid_range/2);
$this->end_range = $this->current_page + floor($this->mid_range/2);

if($this->start_range <= 0):
$this->end_range += abs($this->start_range)+1;
$this->start_range = 1;
if($this->end_range > $this->num_pages):
$this->start_range -= $this->end_range-$this->num_pages;
$this->end_range = $this->num_pages;
$this->range = range($this->start_range,$this->end_range);

if($this->range[0] > 2 And $i == $this->range[0]) $this->return .= " ... ";
// loop through all pages. if first, last, or in range, display
if($i==1 Or $i==$this->num_pages Or in_array($i,$this->range)):
$this->return .= ($i == $this->current_page And $selected_page != 'All') ? "<a title=\"Go to page $i of $this->num_pages\" class=\"current\" href=\"#\">$i</a> ":"<a class=\"paginate\" title=\"Go to page $i of $this->num_pages\" href=\"".$php_self."?page=$i&ipp=$this->items_per_page$this->querystring\">$i</a> ";
if($this->range[$this->mid_range-1] < $this->num_pages-1 And $i == $this->range[$this->mid_range-1]) $this->return .= " ... ";
$this->return .= (($this->current_page != $this->num_pages And $this->items_total >= 10) And ($selected_page != 'All')) ? "<a class=\"paginate\" href=\"".$php_self."?page=$next_page&ipp=$this->items_per_page$this->querystring\">Next &raquo;</a>\n":"<span class=\"inactive\" href=\"#\">&raquo; Next</span>\n";
$this->return .= ($selected_page == 'All') ? "<a class=\"current\" style=\"margin-left:10px\" href=\"#\">All</a> \n":"<a class=\"paginate\" style=\"margin-left:10px\" href=\"".$php_self."?page=1&ipp=All$this->querystring\">All</a> \n";
$this->return .= ($i == $this->current_page) ? "<a class=\"current\" href=\"#\">$i</a> ":"<a class=\"paginate\" href=\"".$php_self."?page=$i&ipp=$this->items_per_page$this->querystring\">$i</a> ";
$this->return .= "<a class=\"paginate\" href=\"".$php_self."?page=1&ipp=All$this->querystring\">All</a> \n";
$this->low = ($this->current_page-1) * $this->items_per_page;
$this->high = ($selected_ipp == 'All') ? $this->items_total:($this->current_page * $this->items_per_page)-1;
$this->limit = ($selected_ipp == 'All') ? "":" LIMIT $this->low,$this->items_per_page";

function display_items_per_page(){
$mod = explode("?", $_SERVER['REQUEST_URI']);
$php_self = $mod[0];
$items = '';
$ipp_array = array(10,25,50,100,'All');
foreach($ipp_array as $ipp_opt) $items .= ($ipp_opt == $this->items_per_page) ? "<option selected value=\"$ipp_opt\">$ipp_opt</option>\n":"<option value=\"$ipp_opt\">$ipp_opt</option>\n";
return "<span class=\"paginate\">Items per page:</span><select class=\"paginate\" onchange=\"window.location='".$php_self."?page=1&ipp='+this[this.selectedIndex].value+'$this->querystring';return false\">$items</select>\n";

function display_jump_menu(){
$mod = explode("?", $_SERVER['REQUEST_URI']);
$php_self = $mod[0];
$option .= ($i==$this->current_page) ? "<option value=\"$i\" selected>$i</option>\n":"<option value=\"$i\">$i</option>\n";
return "<span class=\"paginate\">Page:</span><select class=\"paginate\" onchange=\"window.location='".$php_self."?page='+this[this.selectedIndex].value+'&ipp=$this->items_per_page$this->querystring';return false\">$option</select>\n";

function display_pages(){
return $this->return;

01-06-2012, 08:06 PM
What line and file is this error from?

01-06-2012, 08:12 PM
the problem is when the functions are called from the class at these lines, keeps thinking it is a non object, but when i check to see if the class is loaded it works fine. Its definitely loaded because I was receiving internal errors in the class about missing $_GET['page'] and $_GET['ipp']. I just plugged in a simple if statement to squash those bugs.

echo $pages->display_pages();
echo $pages->display_items_per_page();
echo $pages->display_jump_menu();

01-06-2012, 11:28 PM
Is this where the error is indicated though by the line and file provided in the error code? It should never make it to this point if $pages isn't a Pagination object as it would fail before this point.
I'm not raising to question whether its loaded, that much we can see since it would have failed prior to this point. I also see no globalization that would cause the problem. So the problem is I can't see what is causing the problem. This is why I'm curious of the page and line number; from here it looks like its neither of these blocks.

01-07-2012, 12:19 AM
Yeah, I figured it out now. I kept moving that line around the code until it broke. As it turned out there was a variable being called in my header that overwrote the $page instance. So as soon as i renamed it differently everything lit up like a christmas tree. Thanks for the help. :thumbsup: