Jump to content

Add Parameter to PHP Class?


unemployment

Recommended Posts

I have a pagination class that uses PHP_SELF to display the proper URLs when generating the pagination buttons.  I need PHP_SELF to be a class parameter so that if the page is not defined in the parameter, then use PHP_SELF.  The reason for this is because I generate the class in an ajax file and it is returning the URL of the ajax file, not of the page I am running the ajax on. I just don't know how to add the parameter to the class.

 

The class is below:

 

<?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;
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($default_ipp)
{
	if((isset($_GET['ipp'])) && ($_GET['ipp'] == 'All'))
	{
		$this->num_pages = ceil($this->items_total/$default_ipp);
		$this->items_per_page = $default_ipp;
	}
	else
	{
		if(!is_numeric($this->items_per_page) OR $this->items_per_page <= 0) $this->items_per_page = $default_ipp;
		$this->num_pages = ceil($this->items_total/$this->items_per_page);
	}

	if(isset($_GET['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;
	$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;
		}
	}

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

	if($this->num_pages > 1)
	{
		$this->return = ($this->current_page != 1 And $this->items_total >= 1) ? "<a class=\"paginate\" href=\"$_SERVER[php_SELF]?page=$prev_page&ipp=$this->items_per_page$this->querystring\">« Previous</a> ":"<!--<span class=\"inactive\" href=\"#\">« 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);

		for($i=1;$i<=$this->num_pages;$i++)
		{
			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 (isset($_GET['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> ";
			}
			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 >= 1) //And ($_GET['page'] != 'All')
		) ? "<a class=\"paginate\" href=\"$_SERVER[php_SELF]?page=$next_page&ipp=$this->items_per_page$this->querystring\">Next »</a>\n":"<!--<span class=\"inactive\" href=\"#\">» Next</span>-->\n";

		//$this->return .= ((isset($_GET['page'])) And ($_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";
	}
	else
	{
		for($i=1;$i<=$this->num_pages;$i++)
		{
			$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> ";
		}
		$this->return .= "<a class=\"paginate\" href=\"$_SERVER[php_SELF]?page=1&ipp=All$this->querystring\">All</a> \n";
	}
	$this->low = ($this->current_page-1) * $this->items_per_page;

	if(isset($_GET['ipp']))
	{
		$this->high = ($_GET['ipp'] == 'All') ? $this->items_total:($this->current_page * $this->items_per_page)-1;
		$this->limit = ($_GET['ipp'] == 'All') ? "":" LIMIT $this->low,$this->items_per_page";
	}
	else
	{
		$this->high = ($this->current_page * $this->items_per_page)-1;
		$this->limit = " LIMIT $this->low,$this->items_per_page";
	}
}

function display_items_per_page()
{
	$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='$_SERVER[php_SELF]?page=1&ipp='+this[this.selectedIndex].value+'$this->querystring';return false\">$items</select>\n";
}

function display_jump_menu()
{
	for($i=1;$i<=$this->num_pages;$i++)
	{
		$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='$_SERVER[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;
}
}

Link to comment
https://forums.phpfreaks.com/topic/255675-add-parameter-to-php-class/
Share on other sites

var $querystring;
var $url;

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

 

when you need to specify the url, check for $this->url being empty, if it is, use php_self, otherwise use $this->url

also, are you aware that this uses oop4 syntax?

yeah, I've seen this class before floating around, really, the main issue is visibility, your variables and functions should have a declared visibility for accessibility reasons. Eventually, I suspect that syntax will be weeded out.

 

Edit: sorry, i should have given you an example..

 

public $items_per_page;
public $items_total;
public $current_page;

 

etc..

 

http://www.php.net/manual/en/language.oop5.visibility.php

Ahh yes, now I know what you're talking about as I have read the documentation and seen examples.  I'm getting an undefined constant error on line 25 for PHP_SELF.  Any idea why?

 

<?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;
var $querystring;
var $url;

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

	if(empty($url))
	{
		$this->url = $_SERVER[php_SELF];
	}
	else
	{
		$this->url = $url;
	}
}

function paginate($default_ipp)
{
	if((isset($_GET['ipp'])) && ($_GET['ipp'] == 'All'))
	{
		$this->num_pages = ceil($this->items_total/$default_ipp);
		$this->items_per_page = $default_ipp;
	}
	else
	{
		if(!is_numeric($this->items_per_page) OR $this->items_per_page <= 0) $this->items_per_page = $default_ipp;
		$this->num_pages = ceil($this->items_total/$this->items_per_page);
	}

	if(isset($_GET['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;
	$prev_page = $this->current_page-1;
	$next_page = $this->current_page+1;
	$url = $this->url;

	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;
		}
	}

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

	if($this->num_pages > 1)
	{
		$this->return = ($this->current_page != 1 And $this->items_total >= 1) ? "<a class=\"paginate\" href=\"$url?page=$prev_page&ipp=$this->items_per_page$this->querystring\">« Previous</a> ":"<!--<span class=\"inactive\" href=\"#\">« 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);

		for($i=1;$i<=$this->num_pages;$i++)
		{
			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 (isset($_GET['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=\"$url?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 >= 1) //And ($_GET['page'] != 'All')
		) ? "<a class=\"paginate\" href=\"$url?page=$next_page&ipp=$this->items_per_page$this->querystring\">Next »</a>\n":"<!--<span class=\"inactive\" href=\"#\">» Next</span>-->\n";

		//$this->return .= ((isset($_GET['page'])) And ($_GET['page'] == 'All')) ? "<a class=\"current\" style=\"margin-left:10px\" href=\"#\">All</a> \n":"<a class=\"paginate\" style=\"margin-left:10px\" href=\"$url?page=1&ipp=All$this->querystring\">All</a> \n";
	}
	else
	{
		for($i=1;$i<=$this->num_pages;$i++)
		{
			$this->return .= ($i == $this->current_page) ? "<a class=\"current\" href=\"#\">$i</a> ":"<a class=\"paginate\" href=\"$url?page=$i&ipp=$this->items_per_page$this->querystring\">$i</a> ";
		}
		$this->return .= "<a class=\"paginate\" href=\"$url?page=1&ipp=All$this->querystring\">All</a> \n";
	}
	$this->low = ($this->current_page-1) * $this->items_per_page;

	if(isset($_GET['ipp']))
	{
		$this->high = ($_GET['ipp'] == 'All') ? $this->items_total:($this->current_page * $this->items_per_page)-1;
		$this->limit = ($_GET['ipp'] == 'All') ? "":" LIMIT $this->low,$this->items_per_page";
	}
	else
	{
		$this->high = ($this->current_page * $this->items_per_page)-1;
		$this->limit = " LIMIT $this->low,$this->items_per_page";
	}
}

function display_items_per_page()
{
	$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='$_SERVER[php_SELF]?page=1&ipp='+this[this.selectedIndex].value+'$this->querystring';return false\">$items</select>\n";
}

function display_jump_menu()
{
	for($i=1;$i<=$this->num_pages;$i++)
	{
		$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='$_SERVER[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;
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.