Jump to content

PHP Pagination with classes


jaimitoc30

Recommended Posts

Hi, I am using the PEAR excel reader to read an excel file.  I made a class to handle the interaction with the EXCEL class that I downloaded.  I want to print the results of the Excel file but using pagination.  This is not an easy task. I will add the main file and the class.

 

Here is the main PHP file

(require_once('loadclass.php');

) is used with the _autoload to load all classes when necessary:

<?
require_once('loadclass.php');
$uploadfileobj= new uploadfileclass();
$excelobj= new excelclass();
if(empty($file)){
$file=$uploadfileobj->movefile($_FILES['uploadedfile']['name'], $_FILES['uploadedfile']['tmp_name']);
$excelobj->leerExcel($file);
$arreglo=$excelobj->asignarExcel();
}
$excelobj->printExcel();
?>

 

 

So far we are good, now here is the class, please pay attention to the printExcel function since there is where I am adding the code for the pagination. I used the method explained here in the tutorial:

 

<?
require_once('reader.php');
class excelclass
{
var $data;
var $cells;
var $norows;
var $nocols;
var $row;
var $arreglo;
var $cols;

//LEER ARCHIVO DE EXCEL
function leerExcel($file)
{
	$data = new Spreadsheet_Excel_Reader(); 
	$data->setOutputEncoding('CP1251'); 
	$data->read($file); 
	$this->cells = $data->sheets[0]['cells'];
	$this->norows=$data->sheets[0]['numRows'];  
	$this->nocols=$data->sheets[0]['numCols'];


}

//ASIGNAR EXCEL A ARREGLO
function asignarExcel()
{
	for($this->row=1;$this->row<=$this->norows;$this->row++)
	{
				for ($this->cols = 1;$this->cols <= $this->nocols;$this->cols++)
				{
				$this->arreglo[$this->row][$this->cols] = $this->cells[$this->row][$this->cols]; 
				}

		}
return $this->arreglo;
}

//IMPRIMIR EXCEL EN TABLAS HTML
function printExcel()
{
	$limit=10;
	$totalrows=$this->norows;

	if(empty($page))
	{ 
        $page = 1; 
    	} 

    	$limitvalue = $page * $limit - ($limit);
    	


	$color=1;
	echo "<table border=0>";
			for($this->row=1;$this->row<=$this->norows;$this->row++) 
			{
			if ($color%2==1){
  			echo"<tr bgcolor='#73B1B7'>";	
			}else{
				echo"<tr>";	
				}			
			for ($this->cols = 1;$this->cols <= $this->nocols;$this->cols++)
			{    
				echo "<td>".$this->arreglo[$this->row][$this->cols];
  					echo"</td>";
			}
			echo"</tr>";
			$color=$color+1;
		}
  		echo "</table>";


  		
  		if($page != 1){  
        $pageprev = $page--; 
         
        echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV".$limit."</a> ");  
    }else{ 
        echo("< PREV"." "); 
    } 

    $numofpages = $totalrows / $limit;  
     
    for($i = 1; $i <= $numofpages; $i++){ 
        if($i == $page){ 
            echo($i." "); 
        }else{ 
            echo("<a href=\"$PHP_SELF?page=$i\">$i</a> "); 
        } 
    } 


    if(($totalrows % $limit) != 0){ 
        if($i == $page){ 
            echo($i." "); 
        }else{ 
            echo("<a href=\"$PHP_SELF?page=$i\">$i</a> "); 
        } 
    } 

    if(($totalrows - ($limit * $page)) > 0){ 
        $pagenext = $page++; 
          
        echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT >"."</a>");  
    }else{ 
        echo("NEXT".$limit);  
    } 

  		

  		
}

}
?>

 

I know this is not easy if it was I wouldnt add this post. I would appreciate any help from you geniuses. Currently when I click on the NEXT page, it will say Invalid Excel file. This message comes from another class I made to handle the file upload:

 

<?
class uploadfileclass
{
var $target_path = "uploads/";

	function movefile($file,$filetmp)
	{ 	
	$this->target_path .= basename($file); 
	$this->validformat(basename($file));
	if(!move_uploaded_file($filetmp, $this->target_path)) 
		{
    	echo "There was an error uploading the file, please try again!\n";
		} 
		else{

    			echo "The file ".  basename($file)." has been uploaded to ".$this->target_path."<br>";
			return $this->target_path;
			}
	}


	function validformat($file)
	{
	if (!ereg('.xls',$file)) {
		die("Invalid Excel file.");
		}	

	}
}
?>

 

 

Thats the error message I get when I try to go to the next page, also, it will print all the array instead of the first 10 values.

 

I would appreciate any assistance with this.

Link to comment
https://forums.phpfreaks.com/topic/72851-php-pagination-with-classes/
Share on other sites

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.