记录不限于使用codeigniter分页的搜索页面

时间:2013-05-27 14:20:36

标签: php codeigniter search pagination limit

场合

我有一个搜索表单,它重定向到我想要实现Codeigniter分页的搜索结果页面。 我的限制不起作用。因此显示的是所有结果,而不是我的$limit = 4;

这是我的控制器代码:

    function searchresults()
    {   

        $this->breadcrumbs->page = array('link'=> base_url().'home/search' ,'title' => 'Bedrijven Zoeken' );            
        $this->breadcrumbs->method = array('link'=> base_url().'home/searchresults' ,'title' => 'Zoekresultaten' );
        $data['breadcrumbs'] = $this->breadcrumbs->get();
        $match = $this->input->post('search');
        if(strlen($this->input->post('cookie')) > 0){ $match2 = $this->input->post('cookie'); } else{ $match2 = '9101'; }
        $data['query'] = $this->bedrijven_model->get_search($match, $match2);

        $limit = 4;
        $this->load->library('pagination');
        $config['base_url'] = base_url().'home/searchresults/';
        $config['total_rows'] = count($data['query']);
        $config['per_page'] = $limit;
        $config['uri_segment'] = 3;

        $this->pagination->initialize($config);

        $data['links'] = $this->pagination->create_links();
        $this->load->view('views/header');
        $this->load->view('views/searchresults', $data);
        $this->load->view('views/footer');
    }

问题

可能是什么问题?我错过了什么吗?


修改

我仍然无法让它发挥作用。这是我到目前为止所做的。

控制器:

    function searchresults()
    {   

        $this->breadcrumbs->page = array('link'=> base_url().'home/search' ,'title' => 'Bedrijven Zoeken' );            
        $this->breadcrumbs->method = array('link'=> base_url().'home/searchresults' ,'title' => 'Zoekresultaten' );
        $data['breadcrumbs'] = $this->breadcrumbs->get();
        $match = $this->input->post('search');
        if(strlen($this->input->post('cookie')) > 0){ $match2 = $this->input->post('cookie'); } else{ $match2 = '9101'; }
        $this->load->library('pagination');
        $config['base_url'] = base_url().'home/searchresults/';
        $config['per_page'] = 4;

        $this->pagination->initialize($config);

        $data['links'] = $this->pagination->create_links();
        //$data['query'] = $this->bedrijven_model->get_search($match, $match2, 2147483647, 0); /*large number as limit to retrieve all the rows*/           
        $data['query_curr'] = $this->bedrijven_model->get_search($match, $match2, $config['per_page'], $this->uri->segment(3) );
        $this->load->view('views/header');
        $this->load->view('views/searchresults', $data);
        $this->load->view('views/footer');
    }

型号:

function get_search($match, $match2, $limit, $offset)
{
    $this->db->limit($limit, $offset);
            //rest of the code
    }

我的观点:

    <?php foreach($query_curr as $item):?>
    <br/>
    <div class="logo1">
    <a href="<?php echo base_url()?>bedrijven/<?= $item['idbedrijven'] ?>"><img src="http://i.ebayimg.com/t/Aluminum-Body-Violin-or-Cello-Makers-Compass-Draw-Shave-Plane-Good-Shape-/00/s/NDYwWDU1MQ==/$(KGrHqJ,!hYF!sl)RwymBQiLq5Cn4Q~~60_35.JPG"></a>
    </div>
        <a href="<?php echo base_url()?>bedrijven/<?= $item['idbedrijven'] ?>"><h3><?= $item['Bedrijfsnaam']   ?></h3></a>
        <p>
        <b>Categorie:</b>
        <?php echo "" . $item['Categorie'] . ", &nbsp;" ; ?>
        </p>
        <small>
            <p><b><?php echo $item['Email'] ?></b></p>
            <p><b>Postcode:</b> <?php echo $item['Postcode'] ?></p>
            <p><b>Plaats:</b> <?php echo $item['Plaats'] ?></p>
            <p><b>Tags:</b></p>
            <p><?php echo $item['Profiel'] ?></p>   
        </small>
        <br/><hr/>
    <?php endforeach;?>
    <br/>
    <?= $this->pagination->create_links(); ?>
    <br />
    <br />

1 个答案:

答案 0 :(得分:1)

当您从数据库进行查询以进行分页时,您必须设置偏移和限制。

你的get_search模型应该是这样的

function get_search($match, $match2, $limit, $offset){
    $this->db->limit($limit, $offset);
    /*Rest of the code*/
}

您当前的查询变为

$data['query'] = $this->bedrijven_model->get_search($match, $match2, 2147483647, 0); /*large number as limit to retrieve all the rows*/

您需要使用偏移和限制查询另一个时间

$data['query_curr'] = $this->bedrijven_model->get_search($match, $match2, $config['per_page'], $this->uri->segment(3) );

这里,$ config ['per_page']是限制,偏移量来自url段(你必须初始化url helper。你需要在视图中使用这个query_curr变量。这是将显示使用分页和正确行数的方法。

最后,您不必在视图中将create_links()作为变量发送。您可以直接在视图中使用它。

<强>更新

将您的searchresult功能更改为此

function searchresults()
{   

    $this->breadcrumbs->page = array('link'=> base_url().'home/search' ,'title' => 'Bedrijven Zoeken' );            
    $this->breadcrumbs->method = array('link'=> base_url().'home/searchresults' ,'title' => 'Zoekresultaten' );
    $data['breadcrumbs'] = $this->breadcrumbs->get();
    $match = $this->input->post('search');
    if(strlen($this->input->post('cookie')) > 0){ $match2 = $this->input->post('cookie'); } else{ $match2 = '9101'; }
    $data['query'] = $this->bedrijven_model->get_search($match, $match2, 2147483647, 0);
    $this->load->library('pagination');
    $config['base_url'] = base_url().'home/searchresults/';
    $config['total_rows'] = count($data['query']);
    $config['per_page'] = 4;

    $this->pagination->initialize($config);

    $data['links'] = $this->pagination->create_links();
    //$data['query'] = $this->bedrijven_model->get_search($match, $match2, 2147483647, 0); /*large number as limit to retrieve all the rows*/           
    $data['query_curr'] = $this->bedrijven_model->get_search($match, $match2, $config['per_page'], $this->uri->segment(3) );
    $this->load->view('views/header');
    $this->load->view('views/searchresults', $data);
    $this->load->view('views/footer');
}