Forum Moderators: coopster
I'm working on a page that has data dependant on a variable passed through a url:
eg. [localhost...]
and displayed with a called GET variable:
$val = $_GET['id']
I've got this working fine, now i'm trying to some pagination working to break up the data. I'm using pagination.class from php.net at the moment. It's working but i've got a slight hitch with the links in the pagination. The include file doesn't appear to be passing the 'id' variable:
eg [localhost...]
Here's the code i'm using in label.php to call pagination.class.php etc..
<?php
//include_once("pagination.class.php"); include main class filw which creates pages
$pagination=new pagination();
$query="SELECT * FROM products WHERE label=$val"; // write the database query
/*
Call function that creates the pages
@param - database query
@param - Total number of records per page
*/$pagination->createPaging($query,12);
while($row=mysql_fetch_object($pagination->resultpage)) {
echo '<div class="floatleft">';
echo '<div class="center_text2"><a href="product.php?id='.$row->id.'"><img src="imgsize.php?h=120&img=images/'.$row->image_url.'" alt="'.$row->name.'" /><br >'.$row->product_id.'</div><div class="center_text3">$ '.$row->price.'.00</div></a>'; // display data
echo '</div>';
}
echo '<p /><table border="0" width="380" align="left">';
echo '<tr><td align="center"><img src="images/trans.png" width="33" height="2">';
$pagination->displayPaging();
echo '</td>';
?>
This is a snippet from the pagination.class.php, which is the included php file, where i'm trying to call the $val variable from the current url:
eg. [localhost...]
/*
function to display the pagination
*/
function displayPaging()
{
$self=$_SERVER['PHP_SELF'];
if($this->openPage<=0) {
$next=2;
}else {
$next=$this->openPage+1;
}
$prev=$this->openPage-1;
$last=$this->pages;
if($this->openPage > 1) {
echo "<a href=$self?id=$val&page=1>First</a>  ";
echo "<a href=$self?id=$val&page=$prev>Prev</a>  ";
}
else {
echo "First  ";
echo "Prev  ";
}
for($i=1;$i<=$this->pages;$i++) {
if($i == $this->openPage)
echo "$i  ";
else
echo "<a href=$self?id=$val&page=$i>$i</a>  ";
}
if($this->openPage < $this->pages) {
echo "<a href=$self?id=$val&page=$next>Next</a>  ";
echo "<a href=$self?id=$val&page=$last>Last</a>  ";
}
else {
echo "Next  ";
echo "Last  ";
}
}
}
I know the input/include is working because when i manually type in the correct id variable in the url
eg. [localhost...]
The page works. but the links in the pagination are't passing the $val = $_GET['id'] variable.
so the links are coming up with:
eg [localhost...]
Leaving "id" blank..
Can anyone help me with this please? I think its something small, but its had me scratching my head for a while now.
Thanks
I believe what you are experiencing is a variable scope [php.net] issue. Basically, inside of your pagination Class, it doesn't know anything about the $val variable, because it was set outside of the class. You could simply add this line as the first line of your displayPaging function:
global $val;
But probably the better way is to pass the $val to the class:
$pagination=new pagination($val);
Which requires a few edits in the class... First add this inside your class definition:
var $val;
Second, add this to your class's constructor function:
$this->val =$val;
And finally, change all the references in the class from $val to $this->val:
echo "<a href=$self?id=".$this->val."&page=1>First</a>  ";
this is what i've changed.
paginator.class.php
<?php
/****
File name : pagination.class.php
Description : Class file which creates the pagination .
Author : Shijith. M
Date : 6th August 2008
****/
class pagination {var $fullresult; // record set that contains whole result from database
var $totalresult; // Total number records in database
var $query; // User passed query
var $resultPerPage; //Total records in each pages
var $resultpage; // Record set from each page
var $pages; // Total number of pages required
var $openPage; // currently opened page
[b]var $val;[/b]
/*
@param - User query
@param - Total number of result per page
*/
function createPaging($query,$resultPerPage)
{
$this->query=$query;
$this->resultPerPage=$resultPerPage;
$this->fullresult=mysql_query($this->query);
$this->totalresult=mysql_num_rows($this->fullresult);
$this->pages=$this->findPages($this->totalresult,$this->resultPerPage);
[b]$this->val =$val;[/b]
if(isset($_GET['page']) && $_GET['page']>0) {
$this->openPage=$_GET['page'];
if($this->openPage > $this->pages) {
$this->openPage=1;
}
$start=$this->openPage*$this->resultPerPage-$this->resultPerPage;
$end=$this->resultPerPage;
$this->query.=" LIMIT $start,$end";
}
elseif($_GET['page']>$this->pages) {
$start=$this->pages;
$end=$this->resultPerPage;
$this->query.=" LIMIT $start,$end";
}
else {
$this->openPage=1;
$this->query .=" LIMIT 0,$this->resultPerPage";
}
$this->resultpage =mysql_query($this->query);
}
/*
function to calculate the total number of pages required
@param - Total number of records available
@param - Result per page
*/
function findPages($total,$perpage)
{
$pages=intval($total/$perpage);
if($total%$perpage > 0) $pages++;
return $pages;
}
/*
function to display the pagination
*/
function displayPaging()
{
$self=$_SERVER['PHP_SELF'];
if($this->openPage<=0) {
$next=2;
}
else {
$next=$this->openPage+1;
}
$prev=$this->openPage-1;
$last=$this->pages;
if($this->openPage > 1) {
echo "<a href=$self?id=[b]".$this->val."[/b]&page=1>First</a>  ";
echo "<a href=$self?id=[b]".$this->val."[/b]&page=$prev>Prev</a>  ";
}
else {
echo "First  ";
echo "Prev  ";
}
for($i=1;$i<=$this->pages;$i++) {
if($i == $this->openPage)
echo "$i  ";
else
echo "<a href=$self?id=[b]".$this->val."[/b]&page=$i>$i</a>  ";
}
if($this->openPage < $this->pages) {
echo "<a href=$self?id=[b]".$this->val."[/b]&page=$next>Next</a>  ";
echo "<a href=$self?id=[b]".$this->val."[/b]&page=$last>Last</a>  ";
}
else {
echo "Next  ";
echo "Last  ";
}
}
}
?>
this is the input code from label.php
<?php
include_once("pagination.class.php"); // include main class filw which creates pages
$pagination=new pagination([b]$val[/b]);
$query="SELECT * FROM products WHERE label=$val"; // write the database query
/*
Call function that creates the pages
@param - database query
@param - Total number of records per page
*/
$pagination->createPaging($query,12);while($row=mysql_fetch_object($pagination->resultpage)) {
echo '<div class="floatleft">';
echo '<div class="center_text2"><img src="imgsize.php?h=120&img=images/'.$row->image_url.'" alt="'.$row->name.'" /><br >'.$row->product_id.'</div><div class="center_text3">$ '.$row->price.'.00</div>'; // display name and age from database
echo '</div>';
}
echo '<p /><table border="0" width="380" align="left">';
echo '<tr><td align="center"><img src="images/trans.png" width="33" height="2">';
$pagination->displayPaging();
echo '</td>';
?>
The links generated by the pagination are still missing the $val/"id" variable from label.php
eg.. [localhost...]
Have I put something in wrong?
Thanks