-1

我的网站主页上有一个搜索栏。当我搜索公司并单击“搜索”时,它会重定向到http://mydomain.com/searchresults页面。(注意:我使用 url 路由删除了控制器名称“home”)

我的网址中没有任何搜索词或结果。这只是一个干净的网址。

当我尝试在该页面中实现分页时,它不起作用,因为当我转到下一页时,搜索项消失了,它显示了我数据库中的所有公司。

如何在此页面添加分页?

我的控制器:

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

        $this->load->view('views/header');
        $this->load->view('views/searchresults', $data);
        $this->load->view('views/footer');
        $data['query'] = $this->bedrijven_model->bedrijven_tags();
    }

我的模型:

function get_search($match, $match2)
{
    if (!isset($_COOKIE['cookie'])) {

        $query = "SELECT * FROM (`bedrijfcategorieen`) 
        JOIN `bedrijven` ON `bedrijfcategorieen`.`idbedrijven` = `bedrijven`.`idbedrijven` 
        JOIN `categorieen` ON `bedrijfcategorieen`.`idcategorieen` = `categorieen`.`idcategorieen` 
        WHERE (`Bedrijfsnaam` LIKE '%".$this->input->post('search')."%' 
        OR `Plaats` LIKE '%".$this->input->post('search')."%' 
        OR `Telefoonnummer` LIKE '%".$this->input->post('search')."%' 
        OR `Email` LIKE '%".$this->input->post('search')."%' 
        OR `Website` LIKE '%".$this->input->post('search')."%' 
        OR `Profiel` LIKE '%".$this->input->post('search')."%' 
        OR `Adres` LIKE '%".$this->input->post('search')."%' 
        OR `Categorie` LIKE '%".$this->input->post('search')."%') 
        AND (Postcode LIKE '%".$this->input->post('cookie')."%')

        GROUP BY `bedrijfcategorieen`.`idbedrijven`";
        $result = $this->db->query($query);

        return $result->result();
}

我的搜索页面:

<form name="input" action="searchresults" method="post">
<input type="search" onchange="validate()" placeholder="Zoeken..." name="search" size="70">
<input type="search" onchange="validate()" required="true" size="14" placeholder="Postcode..." name="cookie" value="<?= $this->input->cookie('postcode', TRUE); ?>" >

<input type="submit" value="Zoeken">

</form>

我的搜索结果页面:

<div id="bigcontent">
<h1>Companies found: <?= count($query); ?></h1>
<?= br(1); ?>
<h2>Company:</h2>
<?= br(1); ?>
<?= $this->pagination->create_links(); ?>
<?= br(2); ?>
<hr/>
<?php foreach($query as $item):?>
    <a href="<?php echo base_url()?>bedrijven/<?= $item->idbedrijven?>"><h3><?= $item->Bedrijfsnaam   ?></h3></a>
    <p><b>Categorie:</b> <?= $item->Categorie ?></p>
    <p><small><?php echo $item->Profiel ?></p>
    <p><b><?php echo $item->Email ?></b></p>
    <p><b>Postcode:</b> <?php echo $item->Postcode ?></p>
    <p><b>Tags:</b></p>
    </small>
    <hr/>
<?php endforeach;?>
<br />
<<<a href="<?php echo base_url() ?>">Terug</a>

我试过类似的东西:

(我在我的 autoload.php 文件中加载了分页库)

$config['base_url'] = 'http://kees.een-site-bouwen.nl/home/searchresults';
$config['total_rows'] = $data['query'];
$config['per_page'] = 4;

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

但它没有用。

我希望很清楚我想要什么。

4

4 回答 4

0

据我所知,codeigniter 有一个分页类,它对任何类型的分页都非常有用。

我可以看到您已经添加了配置参数,但没有初始化分页。您甚至还没有加载分页库。请按照这个来加载和初始化库。

我已经在我的现场网站EduSanjal中使用过它并且效果很好。您可以自行搜索并查看。

我希望这能回答你的问题。

问候。

于 2013-05-02T08:38:17.030 回答
0

As @openrijal mentioned it doesn't seem like you have initialized pagination library correctly. I would first try and follow a simple example such as this one...just to be sure you can paginate through your records.

I think one of the main things is that you are not sending the limit and current page to your model to limit the results that you get back, you are just passing match1/match2. Check the tutorial and look at the guy's "fetch_countries" function in his model.

Once you have simple pagination through all results then attach the search term to your pagination links so you can also pass this to the model (add another parameter) to limit the search results further

于 2013-05-02T08:43:48.453 回答
0

这是我不喜欢codeigniter的原因之一。它承诺会付出一切,但当出现一点点复杂性时就​​会失败。

尽管如此,这是一种肮脏的处理方式。

首先,您需要从GET变量构建查询字符串。但在此之前,您需要取消设置$_GET['page']变量。(CI 自动添加它)。

因此代码变成......

if(isset($_GET['page']))unset($_GET['page']);

现在使用其余搜索条件构建查询。

$q = http_build_query($_GET);

现在将基本网址更改为以下内容:

$config['base_url'] = 'http://kees.een-site-bouwen.nl/home/searchresults?'.$q

而已。


更新

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


        /*--- here comes the part --*/

       if(isset($_GET['page']))unset($_GET['page']);
       $q = http_build_query($_GET);

      // Rest pagination code as shown in CI docs.

        $this->load->view('views/header');
        $this->load->view('views/searchresults', $data);
        $this->load->view('views/footer');
        $data['query'] = $this->bedrijven_model->bedrijven_tags();
    }

在这里,您只能自动加载库。不是配置。您需要将其设置为方法。

于 2013-05-02T08:48:10.800 回答
0

我昨天解决了这个问题。完成这项工作是一项艰巨的任务。有两个人帮助我聊天,所以我们可以在没有太多评论的情况下尝试一些事情。

在此处查看解决方案:$offset not working for pagination codeigniter

还有之前的问题:

使用 codeigniter 分页在搜索页面上不受限制的记录

于 2013-05-30T11:54:44.613 回答