1
votes

I have a table of products. I need to display all the products. I am fetching results on different conditions.

create table `products` (
    `id` double ,
    `category_id` double ,
    `subcategory_id` double ,
    `product_name` varchar (765),
    `product_description` varchar (765),
    `product_viewed` varchar (765),
    `sale_wanted` tinyint (2),
    `added_date` datetime ,
    `updated_date` datetime ,
); 

I need to diplay the results like this

1. The latest products (use of added date)
2. Most Wanted (Sorting by sale_wanted 1 for sale , 2 for wanted)
3. Most Viewed (Sorting by product_viewed)
4. Sorting by Specific Subcategory

All the results should display with pagination. This is all right if i first get the result. But if i walk with pagination links all the condition data is lost and the query fetches the results without any condition. How can i manage This situation. Please i dont need Code i need hints and suggestions. The other thing is that i am using Codeigniter's pagination class.

EDITED

Here is my Model Method i am using

public function getProductsList($per_page=5,$page=0)
{
    $info   =   $this->input->post();

    if(isset($info['type']))
    {
        $type   =   $info['type'];

        if($type == 'most_wanted'){
            $where      =   " AND sale_wanted = 1";
            $order_by   =   " ORDER BY ldc.added_date desc";
        }else if($type == 'most_viewed'){
            $where      =   " ";
            $order_by   =   " ORDER BY ldc.product_viewed desc";            
        }else{
            $where      =   " ";
            $order_by   =   " ORDER BY ldc.added_date desc";                
        }
    }else if(isset($info['sale_wanted'])   AND isset($info['subcategory_id'])){
        $sale_wanted        =   $info['sale_wanted'];
        $subcategory_id =   $info['subcategory_id'];
        $where      =   " AND sale_wanted = $sale_wanted AND ldc.subcategory_id = $subcategory_id";
        $order_by   =   " ORDER BY  ldc.added_date desc";   
    }else if(isset($info['keyword'])){
            $keyword    =   $info['keyword'];
            $search_type    =   $info['search_type'];
            $where      =   " AND ldc.$search_type like '$keyword%'";
            $order_by   =   " ";        
    }else{
            $where      =   " ";
            $order_by   =   " ";            
    }           

    $num    =   0;

    if($page != 0){
        $num    =   ($page * $per_page) - $per_page;
    }


    $sql_query  =   "
                SELECT 
                    ldc.id,
                    ldc.product_name,
                    ldc.product_viewed,
                    DATE_FORMAT(ldc.added_date, '%m/%d/%Y') as added_date,
                    ifnull(dc.name,'Unknown') as category,
                    dpi.product_image           
                FROM default_products AS ldc
                LEFT JOIN default_manufacturers as dm ON dm.id = ldc.manufacturer
                LEFT JOIN default_category as dc ON dc.category_id = ldc.category_id
                LEFT JOIN ((select product_id , product_image from default_product_images group by product_id) as dpi)
                    ON dpi.product_id = ldc.id
                WHERE   approved = 1
                $where
                $order_by
                LIMIT $num,$per_page
        ";

    $query  =   $this->db->query($sql_query);
    return  $query->result();
}   
1
are you going display all 4 kinds in different tables..........Venkata Krishna
no i have only a single view(table) and i dont want to repeat my codeMuhammad Raheel
In single table you will display all the products in normal when user selects those conditions they results will change as per condition. am i right......Venkata Krishna
can you add the code which you are using for retrieving and display in the view file........Venkata Krishna
I think you asked this question previously and you only posted the answer also. Now are you facing again the same problem........Venkata Krishna

1 Answers

1
votes

i would highly recommend these two Free tutorials on codeigniter pagination -
video tutorials, working sample code ( might need to update code to CI 2.1 ), and even some helpful info in the comments.

CodeIgniter from Scratch: Displaying & Sorting Tabular Data http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-displaying-sorting-tabular-data/

CodeIgniter from Scratch: Search Results without Query Strings http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-search-results-without-query-strings-2/