Jump to content

Pagination problem, getting negative limit value when user goes past first page


digitalmartyr

Recommended Posts

Its a simple catalog, where you can view products, and view them by category. Im using pagination to make the information more manageable. but when the user goes past page 1 of entires, my limit values goes negative. im not seeing where im going wrong. here is my code.

 

thanx

 

code:

<?php
require('dbconnect.php');
//
if($_GET['page'])
{
$pagesection = $_GET['page'];
}
if(isset($_GET['cat']))
{
$prodCat = $_GET['cat'];
echo "<br />";
echo "Category: ".$prodCat;
}
//
if (empty($pagenum) || !is_numeric($pagenum)){ $pagenum = 1;}
$pagenum = $_GET['pagenum'];

//make sure page number isnt below 1 or more than maxpages
if($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}
//get sql results and count
//see what cat is called
if($prodCat == 'all')
{
$data = "SELECT * FROM products";	
$query = mysql_query($data);
}
else
{
$data = "SELECT product_name, product_price, product_dscrb, product_img FROM products WHERE category = '$prodCat'";
$query = mysql_query($data);
}
$rows = mysql_num_rows($query);
//number of results per page
$page_rows = 3;

//tells the page number of our last page
$last = ceil($rows/$page_rows);


//sets the range to display in our query

$max = ' LIMIT '.($pagenum-1) * $page_rows.','.$page_rows;
$limit = (($pagenum - 1) * $page_rows);
//echo "limit ".$limit;
//echo "pagenum ".$pagenum;
//echo "pagerow ".$page_rows;
$maxquery = $data.$max;
$qdata = mysql_query($maxquery);
//run query to display results *style for display* 
//add $max to it for limiting result
echo "<table>";

// initiate counter
$i = 0;

while($info = mysql_fetch_array($qdata))
{
$html  = '<div id="productThumb">';
$html .= '<h3>product id: '.$info['product_id'].'</h3>';
$html .= '<h2>product name: '.$info['product_name'].'</h2>';
$html .= '<img height="140" width="100" src="'.$info['product_img'].'"</img>';
$html .= '<br />';
$html .= '<span>Product Describtion '.$info['product_dscrb'].'</span>';
$html .= '</div>';

    if(($i + 1) % 3 == 1){
        echo "<tr><td>$html</td>";
    } elseif(($i + 1) % 3 == 2){
        echo "<td>$html</td>";
    } elseif(($i + 1) % 3 == 0){
        echo "<td>$html</td></tr>";
    }

    // increment counter
    $i++;

    // unset html var
    unset($html);
}

echo "</table>";
/*while($info = mysql_fetch_array($qdata))
{
echo '<div id="productThumb">';
echo '<h3>product id: '.$info['product_id'].'</h3>';
echo '<h2>product name: '.$info['product_name'].'</h2>';
echo '<img height="140" width="100" src="'.$info['product_img'].'"</img>';
echo '<span>Product Describtion '.$info['product_dscrb'].'</span>';
echo '</div>';

}*/
//shows user what page they are on and the total number of pages
echo "<p>--page $pagenum of $last--<p>";

//check if on page 1, if so we dont need a link to the prev page or first page so do nothing
//if we arent on page 1 we generate links to the first page and to the prev pages
if($pagenum == 1)
{

}
else
{
echo "<a href='{$_SERVER['PHP_SELF']}?page=$pagesection&pagenum=1'>
<<-First</a>";
echo " ";
$previous = $pagenum-1;
echo "<a href='{$_SERVER['PHP_SELF']}?page=$pagesection&pagenum=$previous'>
<-Previous</a>";
}

//this does the same above, only checking to see if we are on the last page
//and then generating the NExt and Last link
if($pagenum == $last)
{

}
else
{
$next = $pagenum+1;
echo "<a href='{$_SERVER['PHP_SELF']}?page=$pagesection&pagenum=$next'>Next-></a>";
echo " ";
echo "<a href='{$_SERVER['PHP_SELF']}?page=$pagesection&pagenum=$last'>Last->></a>";
}
?>

not sure if this is the error but im sure this is wrong

if (empty($pagenum) || !is_numeric($pagenum)){ $pagenum = 1;}
$pagenum = $_GET['pagenum'];

 

you make an if condition and you assign the value for that var which is useless

so  $pagenum  will never be set as one unless your get is 1

 

 

I tried this, it gets rid of the neg values in my limit, but now its not displaying information past the first page.

 

$pagenum = $_GET['pagenum'];

if (empty($pagenum) || !is_numeric($pagenum))
{ 
$pagenum = 1;
}
else
{
$pagenum = $_GET['pagenum'];
}

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.