Jump to content

paginating categories problem


graham23s

Recommended Posts

Hi Guys,

 

i have small problem here, when aa user browses a specific category i wanted to paginate THAT category, as a test i set the display per page to 5 so if i have 15 items in the database the links are correct they show:

 

<<< 1 2 3 >>>

 

which is correct but the page still shows all 15 rather than 5 per page

 

code:

 

<?php        
        $get_id = $_GET['cat_id'];
        ## browse by category ###########################################################
        if ($_GET['cat_id']) {
        
        $_GET['cat_id'] = round(abs(mb_strcut($_GET['cat_id'], 0, 10))); 
        
        ## cat query ####################################################################
        $category_query = "SELECT * FROM `categories` WHERE `id`='$get_id'";  
        $category_result = mysql_query($category_query) or die (mysql_error());    
        $category = mysql_fetch_array($category_result) or die (mysql_error());
         
        // a few vars for the cats...///////////////////////////////////////////////////
        $cat_id_2 = $category['id'];
        $cat_name = $category['name'];
                            
        // neat table for category...////////////////////////////////////////////////////
             
        echo '<br /><h4>'.$cat_name.'</h4>';
           
        ## display.../////////////////////////////////////////////////////////////////
			   
    $cat_dis = "SELECT * FROM `uploaded_files` WHERE `cat_id`='$cat_id_2' ORDER BY `date_added` DESC";  
    $cat_res = mysql_query($cat_dis) or die (mysql_error()); 

        ## Pagination start #############################################################
        echo "<center>";
	// If current page number, use it 
	// if not, set one! 

	if(!isset($_GET['page'])){ 
		$page = 1; 
	} else { 
		$page = $_GET['page']; 
	} 

	// Define the number of results per page 
	$max_results = 5; 		

	// Figure out the limit for the query based 
	// on the current page number. 
	$from = (($page * $max_results) - $max_results);  

	// Perform MySQL query on only the current page number's results 

	$sql = mysql_query("SELECT * FROM `uploaded_files` WHERE `cat_id`='$cat_id_2' ORDER BY `date_added` DESC LIMIT $from, $max_results"); 

        ## Pagination start #############################################################

 

this is the top half of my code  (the bottom half returs the correct number of rows so i think the error is here)

 

any help would be appreciated

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/58138-paginating-categories-problem/
Share on other sites

$category_query = "SELECT * FROM `categories` WHERE `id`='$get_id'";

 

If you want a certain number rows, you have to put a limit on the query....

 

This is just a simple example, and contains no error checking:

 

$min = (is_numeric($_GET['page'])) ? $_GET['page'] : 0;
$max = $min + 5; //5 results per page
$category_query = "SELECT * FROM `categories` WHERE `id`= '{$get_id}' LIMIT {$min}, {$max}";

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.