<?
class Split
{
	
	
     /*   var $host="localhost";
        var $dbuser="root"; //database username
        var $dbpasswd=""; //database password
        var $db="premlink";      //name of database
	*/
	
	var $totalrows; //total no ofrows returned by the query
	var $maxrow;  //total no of rows displayed in a page
    	var $sql;	//the main query
	var $error;
	var $totalpages;
	var $ppage;
	var $back_link="";
	var $middle_link="";
	var $next_link="";
	var $rows;
	var $back;
	var $next;
	var $other_parameters=array("");

	function Split($sql,$maxrow,$ppage,$back,$next,$passed_par)
	{
		$this->other_parameters=$passed_par;
		$this->back=$back;
		$this->next=$next;
		$this->sql=$sql;
		$this->maxrow=$maxrow;
		$this->ppage=$ppage;		
		

		//$this->cid= mysql_pconnect($this->host, $this->dbuser, $this->dbpasswd)
		//	  or   die("Error: " . mysql_error());
		
		//mysql_select_db($this->db,$this->cid);

		if($this->maxrow <=0)
		{
			$this->error="Max results in a page is not valid";
			return;
		}	
		$out=mysql_query($sql);
		if (!$out)
		{
		         $this->error="ERROR" . mysql_error();
		         return;
		}

		$this->totalrows=mysql_num_rows($out);
                //total rows=0
		if ($this->totalrows==0)
		{
		         $this->error="No Result returned";
		         return;
		}


		$this->totalpages=(int)(($this->totalrows-1)/$this->maxrow+1);
		$this->display();
		// in the above line we initialize the total no of rows and the sql
	}	
        	


//**********************************************************************************
	function display()
	{

		//in this method only the present page to be displayed will be passed
		//if present page is null the first $maxrows will be printed
		if(($this->ppage=="")|| ($this->ppage <=0))
		{
			$this->ppage=1;
			//if the ppage passed is null or zero or less than zero we
			//show the first page
			$min=0;
			//printf("ppage is null or 0 or less than 0<br>");
			
		}
		else
		{
			
			if($this->ppage > $this->totalpages)
			{
				//if the ppage passed is more than the total pages
				//we display the last page
				$this->ppage=$this->totalpages;
				$min=$this->maxrow * ($this->totalpages-1);
				//printf("ppage is > total pages<br>");				
			}
			else
			{
			//if present page is passed the max and min limit is calculated
			//eg:-if ppage is 2 and maxrow=10 then records from 10 to 20 will be displayed.
			
			        //printf("ppage is is $ppage<br>");
				$temp=$this->ppage-1;
				$min=$temp*$this->maxrow;
				$max=$this->maxrow;
												
			}
		}
		$sqlimit = $this->sql ." "."limit"." ". $min . "," . $this->maxrow;
		//printf("<br>sql is $sqlimit<br>");	
		 
		$this->rows=mysql_query($sqlimit)or die("ERROR" . mysql_error());		
		if(mysql_num_rows($this->rows)==0)
		{
			$this->error="No results";
			exit;
		}
		else
		{
			$this->form_links();
			//$this->rows;
			
			
		}	
			
	}
//**********************************************************************************
	function form_links()
	{
		global $PHP_SELF;
		$param="";
		if(count($this->other_parameters)>0)
		{
			while(list($key,$val)=each($this->other_parameters))
			{
				$param.="&$key=$val";
			}
		}
		$param=htmlspecialchars($param);
		if($this->ppage>1)
		{
			$backpage=$this->ppage-1;	
			if($this->back=="")
				$this->back="Back";
			
			$link="<a href=$PHP_SELF?ppage=$backpage$param>";
			$this->back_link="$link$this->back</a>";
		}
		for($j=1;$j<=$this->totalpages;$j++)
		{
				if($j==$this->ppage)
				{
					 $this->middle_link.="$j&nbsp;&nbsp;";
				}
				else
				{
					$link="<a href=$PHP_SELF?ppage=$j$param>";
        	        $this->middle_link.="$link$j</a>&nbsp;&nbsp;";
				}		
		}//end of for loop
		//echo "$this->ppage,$this->totalpages";
		if($this->ppage<$this->totalpages)
		{
				if(!trim($this->next))
					$this->next="Next";

				$nextpage=$this->ppage+1;	
				$link="<a href=$PHP_SELF?ppage=$nextpage$param>";
		        $this->next_link="$link$this->next</a>";
		}
		//if there is no prev and next link, we are not showing the middle links
		if($this->back_link=="" && $this->next_link=="")
			$this->middle_link="";

	}//end of function form_links
//**********************************************************************************	
//********************************************************************************
}//end of class	

?>
