Pagination Class undefined index error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
timwhelan
Forum Newbie
Posts: 8
Joined: Mon Sep 18, 2006 11:34 am

Pagination Class undefined index error

Post by timwhelan »

Of course another newbie here. Using the Paginator Class from http://www.catchmyfame.com/2007/07/28/f ... ion-class/
I am getting an error - Notice: Undefined index: ipp in /www/content/nasa/html/_test/live_test/paginator.class.php on line 27 and line 37-

Code: Select all

 
<?php
 
class Paginator{
    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 = 25;
    var $querystring;
 
    function Paginator()
    {
        $this->current_page = 1;
        $this->mid_range = 7;
        $this->items_per_page = (!empty($_GET['ipp'])) ? $_GET['ipp']:$this->default_ipp;
    }
 
    function paginate()
    {
        if($_GET['ipp'] == 'All')
        {
            $this->num_pages = ceil($this->items_total/$this->default_ipp);         // LINE 27
            $this->items_per_page = $this->default_ipp;
        }
        else
        {
            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) $_GET['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;                //  LINE 37
        $prev_page = $this->current_page-1;
        $next_page = $this->current_page+1;
 
        if($_GET)
        {
            $args = explode("&",$_SERVER['QUERY_STRING']);
            foreach($args as $arg)
            {
                $keyval = explode("=",$arg);
                if($keyval[0] != "page" And $keyval[0] != "ipp") $this->querystring .= "&" . $arg;
            }
        }
 
I've learned that you need to define the variable but don't think that just putting $ipp = 0 and $page = 0 would work.
Anyone have some thoughts that I can learn from...
Thanks
Tim
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Pagination Class undefined index error

Post by McInfo »

Change line 26

Code: Select all

if($_GET['ipp'] == 'All')
to

Code: Select all

if(isset($_GET['ipp']) && $_GET['ipp'] == 'All')
and line 36

Code: Select all

$this->current_page = (int) $_GET['page'];
to

Code: Select all

$this->current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
Edit: This post was recovered from search engine cache.
Berend van der Hulst
Forum Newbie
Posts: 3
Joined: Tue Aug 30, 2011 8:23 am

Re: Pagination Class undefined index error

Post by Berend van der Hulst »

newbie here, using the same paginator and get the following faults:

Notice: Undefined index: page in C:\wamp\www\test\paginator.class.php on line 84
Notice: Undefined index: page in C:\wamp\www\test\paginator.class.php on line 88
Notice: Undefined index: page in C:\wamp\www\test\paginator.class.php on line 89
Notice: Undefined index: ipp in C:\wamp\www\test\paginator.class.php on line 100
Notice: Undefined index: ipp in C:\wamp\www\test\paginator.class.php on line 101

Thanks for your help
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Pagination Class undefined index error

Post by Celauran »

Berend van der Hulst wrote:newbie here, using the same paginator and get the following faults:

Notice: Undefined index: page in C:\wamp\www\test\paginator.class.php on line 84
Notice: Undefined index: page in C:\wamp\www\test\paginator.class.php on line 88
Notice: Undefined index: page in C:\wamp\www\test\paginator.class.php on line 89
Notice: Undefined index: ipp in C:\wamp\www\test\paginator.class.php on line 100
Notice: Undefined index: ipp in C:\wamp\www\test\paginator.class.php on line 101

Thanks for your help
The reason you're seeing these notices is because the script is trying to access a variable that may not have been set. Without seeing said lines, there's very little else we can do to help you.
Berend van der Hulst
Forum Newbie
Posts: 3
Joined: Tue Aug 30, 2011 8:23 am

Re: Pagination Class undefined index error

Post by Berend van der Hulst »

Thanks for the guick reply, hereby the code from row 81 till end

Code: Select all

	81			// loop through all pages. if first, last, or in range, displa
82				if($i==1 Or $i==$this->num_pages Or in_array($i,$this->range))
83				{
84					$this->return .= ($i == $this->current_page AND $_GET['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=\"$_SERVER[PHP_SELF]?page=$i&ipp=$this->items_per_page$this->querystring\">$i</a> ";
85	}
86				if($this->range[$this->mid_range-1] < $this->num_pages-1 And $i == $this->range[$this->mid_range-1]) $this->return .= " ... ";
87			}
88			$this->return .= (($this->current_page != $this->num_pages And $this->items_total >= 10) And ($_GET['page'] != 'All')) ? "<a class=\"paginate\" href=\"$_SERVER[PHP_SELF]?page=$next_page&ipp=$this->items_per_page$this->querystring\">Next &raquo;</a>\n":"<span class=\"inactive\" href=\"#\">&raquo; Next</span>\n";
89			$this->return .= ($_GET['page'] == 'All') ? "<a class=\"current\" style=\"margin-left:10px\" href=\"#\">All</a> \n":"<a class=\"paginate\" style=\"margin-left:10px\" href=\"$_SERVER[PHP_SELF]?page=1&ipp=All$this->querystring\">All</a> \n";
90		}
91		else
92		{
93			for($i=1;$i<=$this->num_pages;$i++)
94			{
95				$this->return .= ($i == $this->current_page) ? "<a class=\"current\" href=\"#\">$i</a> ":"<a class=\"paginate\" href=\"$_SERVER[PHP_SELF]?page=$i&ipp=$this->items_per_page$this->querystring\">$i</a> ";
96			}
97			$this->return .= "<a class=\"paginate\" href=\"$_SERVER[PHP_SELF]?page=1&ipp=All$this->querystring\">All</a> \n";
98		}
99		$this->low = ($this->current_page-1) * $this->items_per_page;
100		$this->high = ($_GET['ipp'] == 'All') ? $this->items_total:($this->current_page * $this->items_per_page)-1;
101		$this->limit = ($_GET['ipp'] == 'All') ? "":" LIMIT $this->low,$this->items_per_page";
102	}
103
104	function display_items_per_page()
105	{
106		$items = '';
107		$ipp_array = array(10,25,50,100,'All');
108		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";
109		return "<span class=\"paginate\">Items per page:</span><select class=\"paginate\" onchange=\"window.location='$_SERVER[PHP_SELF]?page=1&ipp='+this[this.selectedIndex].value+'$this->querystring';return false\">$items</select>\n";
110	}
111
112	function display_jump_menu()
113	{
114	$option = '';
115		for($i=1;$i<=$this->num_pages;$i++)
116		{
117			$option .= ($i==$this->current_page) ? "<option value=\"$i\" selected>$i</option>\n":"<option value=\"$i\">$i</option>\n";
118		}
119		return "<span class=\"paginate\">Page:</span><select class=\"paginate\" onchange=\"window.location='$_SERVER[PHP_SELF]?page='+this[this.selectedIndex].value+'&ipp=$this->items_per_page$this->querystring';return false\">$option</select>\n";
120	}
121
122	function display_pages()
123	{
124		return $this->return;
125	}
126          }

Greetz
Berend
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Pagination Class undefined index error

Post by Celauran »

At a glance, it looks like they're all notices about $_GET['page']. Changing

Code: Select all

$_GET['page'] == 'All'
to

Code: Select all

isset($_GET['page']) && $_GET['page'] == 'All'
should do the trick.
Berend van der Hulst
Forum Newbie
Posts: 3
Joined: Tue Aug 30, 2011 8:23 am

Re: Pagination Class undefined index error

Post by Berend van der Hulst »

After changing $_GET['page'] == 'All' into isset($_GET['page']) && $_GET['page'] == 'All' the messages for row 84, 88 and 89 were solved.

Now i have still these faults


Notice: Undefined index: ipp in C:\wamp\www\test\paginator.class.php on line 99

Notice: Undefined index: ipp in C:\wamp\www\test\paginator.class.php on line 100


Fatal error: Call to undefined method Paginator::display_pages() in C:\wamp\www\test\example.php on line 99



I tried the following:
Replaced string ($_GET['ipp'] == 'All') into isset($_GET['ipp']) && $_GET['ipp'] == 'All' in row 99 and 100, but than i got the following message


Fatal error: Call to undefined method Paginator::display_pages() in C:\wamp\www\test\example.php on line 99



If i am using this code isset($_GET['ipp'])?$_GET['ipp']:’0' in row 99 en 100

Then I have message:

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in C:\wamp\www\test\paginator.class.php on line 100


I tried to find some solutions but still need your help

php code looks this:

Code: Select all

					

84 this->return .= ($i == $this->current_page AND isset($_GET['page']) && $_GET['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=\"$_SERVER[PHP_SELF]?page=$i&ipp=$this->items_per_page$this->querystring\">$i</a> ";
85	}
86				if($this->range[$this->mid_range-1] < $this->num_pages-1 And $i == $this->range[$this->mid_range-1]) $this->return .= " ... ";
87			}
88			$this->return .= (($this->current_page != $this->num_pages And $this->items_total >= 10) And (isset($_GET['page']) && $_GET['page'] == 'All')) ? "<a class=\"paginate\" href=\"$_SERVER[PHP_SELF]?page=$next_page&ipp=$this->items_per_page$this->querystring\">Next &raquo;</a>\n":"<span class=\"inactive\" href=\"#\">&raquo; Next</span>\n";
89			$this->return .= (isset($_GET['page']) && $_GET['page'] == 'All') ? "<a class=\"current\" style=\"margin-left:10px\" href=\"#\">All</a> \n":"<a class=\"paginate\" style=\"margin-left:10px\" href=\"$_SERVER[PHP_SELF]?page=1&ipp=All$this->querystring\">All</a> \n";
90		//else
91		{
92			for($i=1;$i<=$this->num_pages;$i++)
93			{
94				$this->return .= ($i == $this->current_page) ? "<a class=\"current\" href=\"#\">$i</a> ":"<a class=\"paginate\" href=\"$_SERVER[PHP_SELF]?page=$i&ipp=$this->items_per_page$this->querystring\">$i</a> ";
95			}
96			$this->return .= "<a class=\"paginate\" href=\"$_SERVER[PHP_SELF]?page=1&ipp=All$this->querystring\">All</a> \n";
97		}
98		$this->low = ($this->current_page-1) * $this->items_per_page;
99        $this->high = (isset($_GET['ipp'])?$_GET['ipp']:’0') ? $this->items_total:($this->current_page * $this->items_per_page)-1;
100       $this->limit = (isset($_GET['ipp'])?$_GET['ipp']:'0') ? "":  "LIMIT $this->low,$this->items_per_page" ;
101	}
102
103	function display_items_per_page()
104	{
105		$items = '';
106		$ipp_array = array(10,25,50,100,'All');
107		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";
108		return "<span class=\"paginate\">Items per page:</span><select class=\"paginate\" onchange=\"window.location='$_SERVER[PHP_SELF]?page=1&ipp='+this[this.selectedIndex].value+'$this->querystring';return false\">$items</select>\n";
109	}
110
111	function display_jump_menu()
112	{
113	$option = '';
114		for($i=1;$i<=$this->num_pages;$i++)
115		{
116			$option .= ($i==$this->current_page) ? "<option value=\"$i\" selected>$i</option>\n":"<option value=\"$i\">$i</option>\n";
117		}
118		return "<span class=\"paginate\">Page:</span><select class=\"paginate\" onchange=\"window.location='$_SERVER[PHP_SELF]?page='+this[this.selectedIndex].value+'&ipp=$this->items_per_page$this->querystring';return false\">$option</select>\n";
119	}
120
121	function display_pages()
122	{
123		return $this->return;
124	}
125 }
126 }
Post Reply