使用codeigniter分页在搜索页上限制的记录 [英] Records not limited on searchpage using codeigniter pagination

查看:84
本文介绍了使用codeigniter分页在搜索页上限制的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况



我有一个searchform,它会重定向到我要在其中实现Codeigniter分页的searchresults页面。
我的限制不工作。所以所有的结果显示代替我的 $ limit = 4;



这里是我的控制器代码: p>

  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');
}

问题


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






EDIT



不能让它工作。这是我到目前为止。



控制器:

  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); / *大数作为限制检索所有行* /
$ 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-> );
//其余代码
}

我的意见:

 <?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/NDYwWDU1MQew/$(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>分类:< / b>
<?php echo。 $ item ['Categorie']。 ,& nbsp; ; >
< / p>
< small>
< p>< b><?php echo $ item ['Email']?>< / b>< / p&
< p>< b>邮政编码:< / b> <?php echo $ item ['Postcode']?>< / p>
< p>< b> Plaats:< / b> <?php echo $ item ['Plaats']?>< / p>
< p>< b>标签:< / b>< / p>
< p><?php echo $ item ['Profiel']?>< / p>
< / small>
< br />< hr />
<?php endforeach;?>
< br />
<?= $ this-> pagination-> create_links(); >
< br />
< br />


解决方案

您必须设置偏移和限制数据库使分页工作。



您的get_search模型应该像这样

  function get_search($ match,$ match2,$ limit,$ offset){
$ this-> db-> limit($ limit,$ offset);
/ *其余代码* /
}

  $ data ['query'] = $ this-> bedrijven_model-> get_search($ match,$ match2,2147483647 ,0); / *大数字作为限制检索所有行* / 

并限制为这样

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

这里,$ config ['per_page']是限制,偏移量取自url段(你必须初始化url帮助器,你需要在视图中使用这个 query_curr 变量,这是使用分页的方式,并显示正确的行数。 p>

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



UPDATE:



将您的 searchresult / p>

  function searchresults()
{

$ this-> breadcrumbs-> page = array('link'=> base_url()。'home / search','title'=>'Bedrijven Zoeken');
$ this-> breadcrumbs-> method = array '=> base_url()。'home / searchresults','title'=>'Zoekresultaten');
$ data ['breadcrumbs'] = $ this-> breadcrumbs-&
$ 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)
$ it-> 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); / *大数作为限制检索所有行* /
$ 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');
}


Situation

I have a searchform which redirects to a searchresults page in which I want to implement Codeigniter Pagination. My limit is not working. So all the results are shown instead of my $limit = 4;

Here is my controller code:

    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');
    }

Question

What could be the problem? Am I missing something?


EDIT

I still can't get it to work. this is what i have so far.

Controller:

    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');
    }

Model:

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

My views:

    <?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 />

解决方案

You have to set offset and limit when you query from the database to make pagination work.

your get_search model should be like this

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

your current query becomes

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

You need to query another time with offset and limit like this

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

Here, $config['per_page'] is the limit and the offset is taken from the url segment( You have to initialize url helper. You need to use this query_curr variable in the view. This is the way to use pagination and correct number of rows will be shown.

Last thing, you don't have to send create_links() as variable in the view. You can use that directly in the view.

UPDATE:

Change your searchresult function to this

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');
}

这篇关于使用codeigniter分页在搜索页上限制的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆