CodeIgniter中的搜索结果分页不起作用

时间:2018-09-13 10:56:09

标签: codeigniter search pagination

搜索结果未分页。

例如,我将表格视图限制为5行,如果超过5行,则下一个数据将在另一页上(将被分页)。但是我的问题是,每当我在桌子上搜索时,它只会显示五个结果,而其他相关结果却没有分页。

我想显示分页的搜索结果,而不是仅显示它搜索的前五个数据。

很抱歉,如果我的解释不清楚,但是我尽力解释了我遇到的问题。

控制器

public function view_subjects($offset = 0){
        $this->load->config('pagination');
        $config['base_url'] = 'http://localhost/sis_se/index.php/Admin_subjects_controller/view_subjects/';
        $config['per_page'] = 5;    
        // echo $config['total_rows'];

        if (isset($_POST ['search']))
        {
            $config['total_rows'] = count($this->M_admin_subjects->get_all_search($offset, $config['per_page'], $_POST['search']));
            $data['subjects'] = $this->M_admin_subjects->get_all_search($offset, $config['per_page'], $_POST['search']);

        }
        else
        {
            $config['total_rows'] = count($this->M_admin_subjects->get_all_subjects());
            $data['subjects'] = $this->M_admin_subjects->get_all_subjects($offset, $config['per_page']);
        }
        $this->pagination->initialize($config); 
        $this->load->view('admin/subjects/view_admin_subjects', $data);
    }

模型

public function get_all_subjects($offset = 0, $limit = 0)
    {
        $this->db->limit($limit, $offset);
        $query = $this->db->get('subjects');
        return $query->result();
    }
    public function get_all_search($offset = 0, $limit = 0, $val)
    {
        $array = array('code' => $val, 'description' => $val, 'units' => $val, 'lecture_hour' => $val, 'lab_hour' => $val);
        $this->db->like('subject_title', $val);
        $this->db->or_like($array);

        $this->db->limit($limit, $offset);
        $query = $this->db->get('subjects');
        return $query->result();
    }

查看

 <div class="five wide right aligned column">
                <form method = "POST" action= "<?=base_url()?>index.php/Admin_subjects_controller/view_subjects"> 
                    <div class="ui action left icon input">
                        <i class="search icon"></i>
                        <input type ="text" placeholder="Search..." name="search">
                        <input class="ui teal button" type ="submit">
                    </div>
                </form>
            </div>
        </div>

        <table class="ui striped table">
        <thead>
            <th>#</th>
            <th>ID</th>
            <th>Code</th>
            <th>Subject Title</th>
            <th>Description</th>
            <th>Units</th>
            <th>Lecture Hour</th>
            <th>Labaratory Hour</th>
            <th>Action</th>
        </thead>
        <?php 
            if ($this->uri->segment(3)){
                $ctr = $this->uri->segment(3) + 1;
            }
            else
            {
                $ctr = 1;
            }
         ?>
        <tbody> 
        <?php $ctr = 1; ?>
            <?php foreach ($subjects as $subject): ?>
                <tr>
                <td align="center"> <?=  $ctr++ ?> </td>
                <td align="center"> <?=  $subject->id ?> </td>
                <td align="center"> <?=  $subject->code ?></td>
                <td align="center"> <?=  $subject->subject_title ?></td>
                <td align="center"> <?=  $subject->description ?></td>
                <td align="center"> <?=  $subject->units ?></td>
                <td align="center"> <?=  $subject->lecture_hour?></td>
                <td align="center"> <?=  $subject->lab_hour ?></td>
                <td align="center">
                <div class="ui buttons">
                            <a data-position="left center" data-tooltip="Edit" class="ui blue animated fade button" tabindex="0" href="<?= base_url() ?>index.php/Admin_subjects_controller/edit_subject/<?= $subject->id?>">
                                <div class="hidden content">Edit</div>
                                <div class="visible content">
                                <i class="edit icon"></i>
                                </div>
                            </a>
                            <a class="ui red animated fade button" tabindex="0" href="<?= base_url() ?>index.php/Admin_subjects_controller/delete_subject/<?= $subject->id?>">
                                    <div class="hidden content">Delete</div>
                                    <div class="visible content">
                                    <i class="trash alternate icon"></i>
                                    </div>
                            </a>
                </div>

                </td>
                </tr>

            <?php endforeach; ?>
            </tbody>
        </table>
        <?php echo $this->pagination->create_links();?>
    </div>

0 个答案:

没有答案