Jump to content

Problem with pagination, after first page, all other pages fail


digitalmartyr

Recommended Posts

ok so what i have going on here is a catalog system where users can go and view products and view them by order category. each page displays no more than 9 entires at a time thru pagination.

 

the thing is, when you try and view page 2 or results or press the next link, i get an error....  Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource. so i figure it has to do something with the pagenum variable, but dont know how to fix it.

 

 

heres my code.

 

thanx for the help.

<?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>";
}
?>

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.