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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.