<?php
#//////////////////////////////////////////////////////////////////////////////////////////
# Pagination class version 1.0
# This class implements pagination for the data retrieved from a mysql database table
# Coded by Habeeb
# ////////////////////////////////////////////////////////////////////////////////////////



class pagination_noseo
{
	var $retrieve_query, $count_query, $display, $position, $page, $start, $query, $num_pages;
	

	function pagination_noseo($r_query='No', $count_query='No') 
	{
		$this->retrieve_query=$r_query;
		$this->count_query=$count_query;
		
		
	}

	function createPages($disp=10) 
	{
			$this->display=$disp;
		
		$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
		mysqli_select_db($con, DB_NAME) or die(mysqli_error());
			
			if($this->count_query != 'No')
			{
				$num_records_result=mysqli_query($con, $this->count_query) or die(mysqli_error());
		
				$num_records_row=mysqli_fetch_row($num_records_result);
				$num_records=$num_records_row[0];
			} else {
				$num_records=mysqli_num_rows(mysqli_query($con, $this->retrieve_query));
			}

			$this->num_pages=ceil($num_records/$this->display);

			if((isset($_GET['p'])) && (strlen(trim($_GET['p'])) > 0) && (is_numeric(trim($_GET['p']))) && (trim($_GET['p']) > 0) && (trim($_GET['p']) <= $this->num_pages))
			{
				$this->page=trim($_GET['p']);
				$this->start=($this->page-1)*$this->display;
			} else {
				$this->page=1;
				$this->start=0;
			}

			$this->query="$this->retrieve_query".' LIMIT '.$this->start.', '.$this->display;
			return 	mysqli_query($con, $this->query);
	}


	function nextLink($url)
	{
		if($this->page < $this->num_pages) {
			$nextlink = "$url"."p=".($this->page+1)."/";
		} else {
			$nextlink = 0;
		}

		return $nextlink;
	}


	function pageNumber($disp_pages=10, $url)
	{
	if($this->num_pages > 1) {	
		$pg_number='';
		$t_num=$disp_pages;
		
		$pg_number.='<ul class="pagination">';
		if($this->page > 1)
		{
			$pg_number.="<li><a href=\"$url"."p=".(1)."\"><i class=\"fa fa-caret-left\"></i><i class=\"fa fa-caret-left\"></i></a></li>";
		} else {
			$pg_number.="<li class=\"disabled\"><a onclick=\"return false;\" href=\"#\"><i class=\"fa fa-caret-left\"></i><i class=\"fa fa-caret-left\"></i></a></li>";
		}
		
		if($this->page > 1) {
			$pg_number.= "<li><a href=\"$url"."p=".($this->page-1)."\"><i class=\"fa fa-caret-left\"></i></a></li>";
		} else {
		$pg_number.="<li class=\"disabled\"><a onclick=\"return false;\" href=\"#\"><i class=\"fa fa-caret-left\"></i></a></li>";
		}

		if($this->page <= floor($t_num/2)) {
			$sn=1;
			$en=$t_num;
		} elseif($this->page <= $this->num_pages) {
			$sn=$this->page-floor($t_num/2);
			$en=$this->page+floor($t_num/2);
		}
		if($en >= $this->num_pages)       
		{
			$sn=$this->num_pages-($t_num-1);
			$en=$this->num_pages;

		}
		if(($en-$t_num) != $t_num) {
			$sn=$en-$t_num+1;
		} elseif(($en-$t_num) != 0) {
			$sn=$en-$t_num+1;
		}
		if($sn < 1) {
			$sn=1;
		}



		for($i=$sn; $i<=$en; $i++)
		{
			if($i == $this->page)
			{
				$pg_number.= '<li class="active"><a onclick="return false;" href="#">'.$i.'</a></li>';
			} else {
				$pg_number.= '<li><a href="'.$url.'p='.$i.'">'.$i.'</a></li>';
			}
		}
		
		if($this->page < $this->num_pages) {
			$pg_number.= "<li><a href=\"$url"."p=".($this->page+1)."\"><i class=\"fa fa-caret-right\"></i></a></li>";
		} else {
			$pg_number.="<li class=\"disabled\"><a href=\"#\" onClick=\"return false\"><i class=\"fa fa-caret-right\"></i></a></li>";
		}
	 	
		if($this->page < $this->num_pages)
		{
			$pg_number.= "<li><a href=\"$url"."p=".($this->num_pages)."\"><i class=\"fa fa-caret-right\"></i><i class=\"fa fa-caret-right\"></i></a></div>";
		} else {
			$pg_number.="<li class=\"disabled\"><a href=\"#\" onClick=\"return false\"><i class=\"fa fa-caret-right\"></i><i class=\"fa fa-caret-right\"></i></a></li>";
		}
		$pg_number.='</ul>';
	} else {
		$pg_number='';
	}

		return $pg_number;

	}
}
?>