codeigniter分页搜索不起作用

时间:2014-04-06 10:03:02

标签: php codeigniter pagination

这里控制器调用搜索功能并执行分页。我正在获取输出,但搜索结果的分页不起作用,因为当我点击分页的前向链接时它可以工作但是当我点击分页链接时它将显示数据库的信息而不是搜索到的信息。我刚刚在codeigniter工作,我不知道该怎么做。

Controller(main.php)

    public function search()
    {
      if($this->session->userdata('is_logged_in'))
      {  $this->load->model('model_users');

         $config = array();
    $config["base_url"] = base_url() . "main/search";
    $config["total_rows"] = $this->model_users->get_count();
    $config["per_page"] = 2;
    $config["uri_segment"] = 3;
    $config['next_link'] = '>';
    $config['prev_link'] = '<';


    $this->pagination->initialize($config);
     //  $data['offset_no'] = $this->uri->segment(3);
    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
    $match = $this->input->post('search');
    $data["results"] = $this->model_users->
        get_search($config["per_page"], $page, $match);
    $data["links"] = $this->pagination->create_links();

   $this->load->view('search', $data);
      }
      else
      {
        redirect('main/restricted');
      }
   }

模型(model_users.php)

    public function get_search($limit, $start, $match)
   {
    $this->db->limit($limit, $start);


    $this->db->select('*');
   $this->db->from('users');
   $this->db->like('name',$match, 'before');
    $this->db->or_like('address',$match, 'before');
    $this->db->or_like('email',$match, 'before');
    $query = $this->db->get();

  if($query->num_rows() > 0)
  {

    foreach($query->result() as $row)
  {
     $data[]= $row;
  }
     return $data;
     }
     else return False;
     }

      public function get_count()
     {

        $match = $this->input->post('search');
                             $this->db->select('*');
             $this->db->from('users');
           $this->db->like('name',$match, 'before');
            $this->db->or_like('address',$match, 'before');
          $this->db->or_like('email',$match, 'before');
           $query = $this->db->get();


             return count($query->result());





     }

查看:search.php

           <div class="search">
       <?=form_open('main/search');?>
        <?php $search = array(
          'name'=>'search',
         'id' => 'search',

         'style'       => 'height:25px',
        'style'       => 'align:center',
         'value'       => $this->input->post('search')
         );
               ?>
                  <?=form_input($search);?><input type=submit value='Search' /></p>
          <?=form_close();?>

            </div>
         <?php
                 if($results)  {

           echo "<table border='1'>
        <tr>
            <th> USER_ID  </th>
         <th> USER_NAME</th>
              <th> EMAIL</th>

        <th> USER_ADDRESS</th>
          <th> CONTACT_NO</th>
        <tr> ";
     foreach($results as $row){
       echo "<tr>";
      echo "<td>" .$row->id. "</td>";
        echo "<td>" .$row->name. "</td>";
     echo "<td>" .$row->email. "</td>";
       echo "<td>" .$row->address. "</td>";
      echo "<td>" .$row->contact_no. "</td>";


         echo"</tr>";
         }
           echo"</table>";
            }
         else
       {
        echo "No results was found in the table for
       this keyword " .$this->input->post('search');  }             








              ?>
                     <p><?php echo $links; ?></p>

1 个答案:

答案 0 :(得分:0)

试试这个

<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
   <tr>
    <th>#</th>
  </tr>
</thead>
<tbody>
<tr>
<td>###</td>
</tr>
</tbody>