NameSilo

PHP Page Generator

Namecheap AuctionsNamecheap Auctions
SpaceshipSpaceship
NamecheapNamecheap
Watch

xrvel

i love automationVIP Member
Impact
165
Hi,

Here is a simple class to generate pages (for paging) for website.

Class source
PHP:
<?php
class page_generator {

	var $current_page_format;
	var $go_next_format;
	var $go_previous_format;
	var $hyperlink_format;
	var $page_url_format;

	var $all_records;
	var $current_page;
	var $flag;
	var $item_per_page;
	var $max_displayed_pages;
	var $page_parameter;
	var $page_string_next;
	var $page_string_previous;
	var $temp;

	function page_generator() {
		$this->init();
	}

	function set_all_records($i) {
		$this->all_records = $i;
	}

	function get_limit_clause() {
		$this->read_current_page();
		return array(
			'from' => (($this->temp['current_page']-1)*$this->item_per_page),
			'to' => $this->item_per_page
		);
	}

	function get_page_string() {

		$i = 1;

		$this->read_current_page();

		$s = '';

		$pagenum = ceil($this->all_records/$this->item_per_page);


		if ($this->temp['current_page'] > 1) {
			$s .= sprintf($this->go_previous_format,sprintf($this->page_url_format,$this->page_parameter.'='.($this->temp['current_page']-1)));
		}

		$temp = range(1, $pagenum);

		$temp = array_chunk($temp, $this->max_displayed_pages);
		$temp_max = count($temp);
		for ($i = 0;$i<$temp_max;$i++) {
			if (in_array($this->temp['current_page'], $temp[$i])) {
				$min_draw = $temp[$i][0];
				$max_draw = $temp[$i][count($temp[$i])-1];
				break;
			}
		}

		for ($i=1;$i<=$pagenum;$i++) {
			if ($min_draw <= $i && $i <= $max_draw) {
				if ($i != $this->temp['current_page']) {
					$s .= sprintf($this->hyperlink_format,sprintf($this->page_url_format,$this->page_parameter.'='.$i),$i);
				} else {
					$s .= sprintf($this->current_page_format,$i);
				}
			}
			if ($i > $max_draw) {
				break;
			}
		}

		if ($this->temp['current_page']<$pagenum && $pagenum!=0) {
			$s .= sprintf($this->go_next_format,sprintf($this->page_url_format,$this->page_parameter.'='.($this->temp['current_page']+1)));
		}
		return(trim($s));
	}

	function init() {
		$this->all_records = 0;
		$this->current_page = 0;
		$this->current_page_format = '  <b>%s</b>  ';
		$this->go_next_format = ' <a href="%s">»</a> ';
		$this->go_previous_format = ' <a href="%s">«</a> ';
		$this->flag = array();
		$this->flag['ALL_RECORDS'] = false;
		$this->hyperlink_format = '  <a href="%s">%s</a>  ';
		$this->item_per_page = 5;
		$this->max_displayed_pages = 5;
		$this->page_parameter='page';
		$this->page_string_next='Next';
		$this->page_string_previous='Previous';
		$this->page_url_format='';
		$this->temp = array();
		$this->temp['current_page'] = 1;
	}

	function my_intval(&$s) {
		$s = intval($s);
	}

	function my_trim(&$s) {
		$s = trim($s);
	}

	function read_current_page() {
		if (isset($_GET[$this->page_parameter])) {
			$page=intval($_GET[$this->page_parameter]);
		} else {
			$page=1;
		}
		if ($page < 1) {
			$page = 1;
		}
		$this->temp['current_page'] = $page;
	}

	/*
	 * Get & set
	 */

	function current_page_format($s='') {
		$this->my_trim($s);
		if ($s=='') {
			return(trim($this->current_page_format));
		} else {
			$this->current_page_format=$s;
		}
	}

	function go_next_format($s='') {
		$this->my_trim($s);
		if ($s=='') {
			return(trim($this->go_next_format));
		} else {
			$this->go_next_format=$s;
		}
	}

	function go_previous_format($s='') {
		$this->my_trim($s);
		if ($s=='') {
			return(trim($this->go_previous_format));
		} else {
			$this->go_previous_format=$s;
		}
	}

	function hyperlink_format($s='') {
		$this->my_trim($s);
		if ($s=='') {
			return(trim($this->hyperlink_format));
		} else {
			$this->hyperlink_format=$s;
		}
	}

	function item_per_page($s='') {
		$this->my_trim($s);
		if ($s=='') {
			return(intval($this->item_per_page));
		} else {
			$this->item_per_page=intval($s);
		}
	}

	function max_displayed_pages($s='') {
		$this->my_trim($s);
		if ($s=='') {
			return(intval($this->max_displayed_pages));
		} else {
			$this->max_displayed_pages=intval($s);
		}
	}

	function page_parameter($s='') {
		$this->my_trim($s);
		if ($s=='') {
			return(trim($this->page_parameter));
		} else {





			$this->page_parameter=$s;
		}
	}

	function page_url_format($s='') {
		$this->my_trim($s);
		if ($s=='') {
			return(trim($this->page_url_format));
		} else {
			$this->page_url_format=$s;
		}
	}


}
?>

Usage :
For example :
  1. Normal SQL query is
    Code:
    SELECT * FROM example
  2. You want each page to display 8 data.
  3. You want to display max 10 page links (to avoid too long page link)
  4. The name of your current file is "display_data.php"

You can use this :
PHP:
<?php
// Let's get the total number of data
$q = "SELECT COUNT(*) FROM example";
$q = mysql_query($q);
$r = mysql_fetch_row($q);
$total_data = $r[0];

// Now here is the page generator
$pg = new page_generator();
$pg->set_all_records($total_data);
$pg->item_per_page(8);
$pg->max_displayed_pages(10);
$pg->page_url_format("display_data.php?%s");//page location
$page_navigation = $pg->get_page_string();// here is the page link
$limit_clause = $pg->get_limit_clause();// limit clause
unset($pg);
?>

Next, just before your code to read data from database
PHP:
<?php
$q = "SELECT * FROM example LIMIT ".$limit_clause['from']." , ".$limit_clause['to'];
$q = mysql_query($q);
?>

And you can display the page link anytime with this code :)
PHP:
<?php echo $page_navigation; ?>

I hope this code can help.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Appraise.net

We're social

Escrow.com
Spaceship
Escrowly
CryptoExchange.com
Domain Recover

NamePros updates

New in Parking: updated way to act on many domains or inquiries at once
Your domain list and inquiry list now have a bar above and below the list. Check the items you want, then choose an action in the bar...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back