具有动态参数的Codeigniter分页和MySQL查询

时间:2012-09-26 10:10:31

标签: php mysql codeigniter pagination

让我详细解释一下我的问题

我有一个名为products

的表
Products : Name , views , sale_wanted , added_date

我在视图中列出了我的所有产品 我通过旗帜处理销售意味着0或1 有一些链接可以对列表进行排序。

Most Viewed    
Wanted    
For Sale    
New Arrivals    

现在,当用户点击时,所有列表都根据我发送的参数进行排序 我想使用Codeigniter的分页类 这里有一些问题 当我点击时,假设想要并发送一个参数,它列出了所有想要的产品。

Now i click on a pagination link and the wanted parameter is gone 
and the list becomes without wanted parameter.    

与其他主播一样 我必须限制它,所以它仍然有我的参数 第二个问题是我所看到的Codeigniter正在徘徊 我需要一些用户可以选择的链接。意味着我想让用户有权选择他想在一个页面中看到多少产品。

Let suppose 5 ,10 ,15 ,20    

选择较大的数字会减少页数 我仍然想要相同的功能。
重要的是,我想一次性处理所有这些意味着我不想为每个锚重复我的代码。 我需要专家的建议以及有关分页库的任何帮助。

2 个答案:

答案 0 :(得分:0)

$seg=3
$noOfresultsToshow=$this->uri->segment('5')//use if else for by default no of results
$typeOfresult=$this->uri->segment('4'); //Most Viewed Wanted For Sale    New Arrivals
$config['total_rows'] = $countqueryresults;// mention the total rows here
$config['per_page'] = $noOfresultsToshow;
$config['uri_segment'] = $seg;
$this->pagination->initialize($config);
$data['yourvariable'] = $this->model_name->getData($config['per_page'], $this->uri->segment($seg),$typeOfresult);
$data['links'] = $this->pagination->create_links();

在您的视图文件echo $links;

答案 1 :(得分:0)

我找到了解决问题的方法。

我在Codeigniter分页库中创建了两个方法

他们在这里

定义了两个新变量

var $base_link  =   '';
var $per_page_link  =   TRUE;
var $per_page_array =   array();

用户可以在初始化时定义它们

$config['per_page_link']    =   TRUE;
$config['per_page_array']   =   array(5,10,15,20);
$this->pagination->initialize($config);

现在方法

public function create_per_page()
{
    $CI =& get_instance();

    $output =   '';
    $current    =   $CI->uri->segment(3,5);

    if($this->per_page_link === TRUE AND count($this->per_page_array) > 1){

        foreach($this->per_page_array as $row){

            if($current == $row){
                $output .=  $this->cur_tag_open . $row . $this->cur_tag_close;
                $output .=  ' ';                   
            }else{
                $output .=  '<a href="'.$this->base_link.$row.'/1">'.$row.'</a>';
                $output .=  '&nbsp;';
            }
        }

    }
    return  $output;
}

另一个是create_per_page_links()。我刚刚复制了create_links方法的代码并对其进行了修改。在create_per_page_links()的开头,我添加了这些额外的行。

$this->per_page =   $CI->uri->segment(3,5);
$this->uri_segment  =   $CI->uri->segment(4,1); 

但是,这只适用于细分受众群。我还没有用查询字符串测试它。此外,如果per_page_link为FALSE,则两种方法都不起作用。 调用create_per_page_links将生成以下内容

« First  < 1 2 3 4 5 >  Last »

create_per_page将生成以下内容

5 10 15 20  

完成