digitalmartyr Posted February 13, 2008 Share Posted February 13, 2008 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>"; } ?> Link to comment https://forums.phpfreaks.com/topic/90836-pagination-problem-getting-negative-limit-value-when-user-goes-past-first-page/ Share on other sites More sharing options...
p2grace Posted February 13, 2008 Share Posted February 13, 2008 You have this elseif ($pagenum > $last) { $pagenum = $last; } Where's the $last variable being set at? Link to comment https://forums.phpfreaks.com/topic/90836-pagination-problem-getting-negative-limit-value-when-user-goes-past-first-page/#findComment-465587 Share on other sites More sharing options...
digitalmartyr Posted February 13, 2008 Author Share Posted February 13, 2008 i define it here $last = ceil($rows/$page_rows); Chad Link to comment https://forums.phpfreaks.com/topic/90836-pagination-problem-getting-negative-limit-value-when-user-goes-past-first-page/#findComment-465588 Share on other sites More sharing options...
teng84 Posted February 13, 2008 Share Posted February 13, 2008 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 Link to comment https://forums.phpfreaks.com/topic/90836-pagination-problem-getting-negative-limit-value-when-user-goes-past-first-page/#findComment-465590 Share on other sites More sharing options...
p2grace Posted February 13, 2008 Share Posted February 13, 2008 Right, but you're defining it below the elseif statement. Link to comment https://forums.phpfreaks.com/topic/90836-pagination-problem-getting-negative-limit-value-when-user-goes-past-first-page/#findComment-465591 Share on other sites More sharing options...
digitalmartyr Posted February 13, 2008 Author Share Posted February 13, 2008 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']; } Link to comment https://forums.phpfreaks.com/topic/90836-pagination-problem-getting-negative-limit-value-when-user-goes-past-first-page/#findComment-465596 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.