【问题标题】:codeIgniter pagination- doesn't go to the next link of searched resultscodeIgniter 分页 - 不转到搜索结果的下一个链接
【发布时间】:2015-07-06 09:29:50
【问题描述】:

我正在对搜索结果使用分页。搜索工作完美。搜索后显示前 10 条记录。但是当我点击下一个按钮时,一切都消失了,并显示了一个空白页面。

我的代码中可能有问题的任何想法都将不胜感激。

型号

function search_bookings($time, $title, $payment, $start_date, $end_date,$limit, $start, $type) {

        $this->db->select('reservations.*');
        $this->db->from('reservations');
        $this->db->where('is_deleted', '0');
        $this->db->order_by('date_cal',"desc");

         if (!empty($time) && !is_null($time)) {
            $this->db->where('reservations.type', $time);
        }
        if (!empty($payment) && !is_null($payment)) {
            $this->db->where('reservations.advanced_payment_status', $payment);
        }

        if (!empty($title) && !is_null($title)) {
            $this->db->where('reservations.title', $title);
        }

        if (!empty($start_date) && !is_null($start_date)) {
            $this->db->where('reservations.date_cal >=', $start_date);
            $this->db->where('reservations.date_cal <=', $end_date);
        }

         if ($type == 'half') {
            $this->db->limit($limit, $start);
        }
        $query = $this->db->get();
        return $query->result();
    }

控制器

 function search_reservations($start = 0) {
        $reservation_service = new Reservation_service();
        $config = array();

        $config["base_url"] = site_url() . "/dashboard/manage_bookings/";
        $config["per_page"] = 10;
        $config["uri_segment"] = 4;
        $config["num_links"] = 4;

        $time = $this->input->post('type', TRUE);
        $title = $this->input->post('title', TRUE);
        $payment = $this->input->post('payment', TRUE);
        $date_from = $this->input->post('date_from', TRUE);
        $date_to = $this->input->post('date_to', TRUE);
        $searched_results = $reservation_service->search_bookings($time, $title, $payment, $date_from, $date_to, $config["per_page"], $start, 'half');
        $data['search_results'] = $searched_results;
        $config["total_rows"] = count($reservation_service->search_bookings($time, $title, $payment, $date_from, $date_to, $config["per_page"], 0, 'all'));
        $this->pagination->initialize($config);

        $data["links"] = $this->pagination->create_links();

        $this->load->view('body_pages/search_results', $data);
    }

搜索结果视图

<table  class="display table table-bordered table-striped" id="bookings_table">
    <thead>
        <tr>
            <th>#</th>
            <th>Date</th>
            <th>Hall</th>
            <th>Time</th>                               
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $i = 0;
        foreach ($search_results as $result) {
            ?>
            <tr id="bookings_<?php echo $result->id; ?>">
                <td><?php echo ++$i; ?></td>
                <td><?php echo $result->date_cal; ?></td>
                <td><?php if ("RP" == $result->title) { ?><?php
                        echo "Royal Princess Ballroom (Downstairs)";
                    }
                    ?>
                    <?php if ("GK" == $result->title) { ?><?php
                        echo "Grand Kings Ballroom (Upstairs)";
                    }
                    ?>
                </td>
                <td><?php echo $result->type; ?></td>  
                <td align="center">    
                    <a class="btn btn-primary btn-xs" onclick="display_edit_reservation_pop_up(<?php echo $result->id; ?>)"><i class="fa fa-pencil"  title="Update"></i></a>
                    <a class="btn btn-danger btn-xs" onclick="delete_bookings(<?php echo $result->id; ?>)" ><i class="fa fa-trash-o " title="Remove"></i></a>
                </td>
            </tr>
        <?php } ?>

    </tbody>

</table>

<div class="pagination">
    <?php echo $links; ?>
</div>                                          

【问题讨论】:

  • $config["uri_segment"] = 2;
  • @Abdulla 这对我不起作用。
  • $config["uri_segment"] = 3; 试试这个
  • @Stell 你过得怎么样?
  • @wolfgang1983 这对我不起作用。当我加载数据库中的所有数据时,分页正在工作。但是当我提供搜索功能时,第一页结果正在显示。但如果我转到搜索结果的下一页,它会再次显示数据库中的所有数据。而不是搜索结果的第二页。

标签: php codeigniter pagination


【解决方案1】:

嘿嗨,请找到下面带有分页类的控制器代码

/* start code of pagination */
        $config                     = array();
        $config["base_url"]         = base_url()."cms/manage_cms";
        if (count($_GET) > 0) $config['suffix'] = '?' . http_build_query($_GET, '', "&");
        if (count($_GET) > 0) $config['first_url'] = $config['base_url'].'?'.http_build_query($_GET);
        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        //$page = 0;
        $config["total_rows"]       = count($this->mdl_cms->getAllCmsByCondition($searchindex,$orderField,$orderby,$rowsPerPage,$page,false));

        if($config["total_rows"] < $rowsPerPage){
            $page = 0;
        }
        $config["per_page"]         = $rowsPerPage;
        //$config["uri_segment"]      = 3;
        $config['full_tag_open']    = '<div class="pagination"><ul>';
        $config['full_tag_close']   = '</ul></div><!--pagination-->';

        $config['first_link'] = '&laquo; First';
        $config['first_tag_open'] = '<li class="prev page">';
        $config['first_tag_close'] = '</li>';

        $config['last_link'] = 'Last &raquo;';
        $config['last_tag_open'] = '<li class="next page">';
        $config['last_tag_close'] = '</li>';

        $config['next_link'] = 'Next &rarr;';
        $config['next_tag_open'] = '<li class="next page">';
        $config['next_tag_close'] = '</li>';

        $config['prev_link'] = '&larr; Previous';
        $config['prev_tag_open'] = '<li class="prev page">';
        $config['prev_tag_close'] = '</li>';

        $config['cur_tag_open']     = '<li class="active"><a href="">';
        $config['cur_tag_close']    = '</a></li>';
        $config['num_tag_open']     = '<li class="page">';
        $config['num_tag_close']    = '</li>';

        $this->pagination->initialize($config);  // Load pagination class 


        $usersArr = $this->mdl_cms->getAllCmsByCondition($searchindex,$orderField,$orderby,$rowsPerPage,$page,true); 
        //echo $this->db->last_query(); //die;
        $data["links"] = $this->pagination->create_links(); // Create pagination links

希望对你有用

【讨论】:

  • 我会试试这个。谢谢
【解决方案2】:

尝试添加

$config['use_page_numbers'] = TRUE;

制作

$config['uri_segment'] = 4;

$config['uri_segment'] = 3;

可能需要设置你的 routes.php

$routes['dashboard/manage_bookings/(:any)'] = "dashboard/manage_bookings/$1"

CI2http://www.codeigniter.com/userguide2/general/routing.html

CI3http://www.codeigniter.com/user_guide/general/routing.html

【讨论】:

  • 我会试试这个。谢谢
【解决方案3】:

您正在从 POST 中获取 $time、$title、$payment、$date_from、$date_to 变量。

分页链接类似于 - home/search_reservations/2

一旦用户点击这些链接,帖子中的搜索数据就会丢失。如果您希望它们影响所有页面,则需要将搜索参数记录在案。

两种方法-

1) 使用 GET 而不是 POST。将搜索参数保留在生成的分页链接的查询字符串中。这样所有页面都将具有搜索参数。如果您想共享/收藏搜索结果的特定页面,也很有用。例如,您的页面将被链接为 -

www.yoursite.com/home/search_reservations/2?time=123&title=123&payment=123&date_from=123&date_to=123 

2) 将搜索参数保留在会话中。代码更混乱,但 URL 更漂亮。

【讨论】:

    猜你喜欢
    • 2014-03-26
    • 2019-10-03
    • 1970-01-01
    • 2017-11-27
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-27
    相关资源
    最近更新 更多