在具有分页的Codeigniter中基于查询显示结果

时间:2012-11-20 09:45:32

标签: codeigniter pagination

我有一张产品表。我需要显示所有产品。我在不同条件下获取结果。

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 ,
); 

我需要像这样显示结果

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

所有结果都应以分页显示。如果我第一次得到结果,这没关系。但是,如果我使用分页链接,所有条件数据都会丢失,并且查询会在没有任何条件的情况下获取结果。我该如何处理这种情况。请我不需要Code我需要提示和建议。另一件事是我正在使用Codeigniter的分页类。

EDITED

这是我正在使用的模型方法

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 个答案:

答案 0 :(得分:1)

我强烈推荐这两个关于codeigniter分页的免费教程 -
视频教程,工作示例代码(可能需要将代码更新到CI 2.1),甚至评论中的一些有用信息。

Scratch的CodeIgniter:显示&对表格数据进行排序 http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-displaying-sorting-tabular-data/

Scratch的CodeIgniter:没有查询字符串的搜索结果 http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-search-results-without-query-strings-2/