Codeigniter:搜索分页不受post方法的限制?

时间:2016-09-06 09:18:37

标签: php codeigniter

我的控制器:

public function search()
    {   
        $data['category_name'] = $this->model_products->dis_category();
            $this->load->view('layout/header', $data);



            $keyword = ''; // default when no term in session or POST
            if ($this->input->post('search'))
            {
                // use the term from POST and set it to session
                $keyword = $this->input->post('search');
                $this->session->set_userdata('keyword', $keyword);
            }
            elseif ($this->session->userdata('keyword'))
            {
                // if term is not in POST use existing term from session
                $keyword = $this->session->userdata('keyword');
            }

            $data['otherproduct'] = $this->model_products->other_prodects();
            $data['otherproduct2'] = $this->model_products->other_prodects2();

            //Pagination init
            $config['base_url']     = base_url().'search/';
            $config['total_rows']   = $this->model_products->num_rows($keyword);
            $config['per_page']     = 10;
            $config['uri_segment']  = 2;
            $config['num_links']    = 2;
            //custom pigination
            $config['full_tag_open'] = '<ul class="tsc_pagination tsc_paginationA tsc_paginationA01">';
            $config['full_tag_close'] = '</ul>';
            $config['prev_link'] = 'Prev';
            $config['prev_tag_open'] = '<li>';
            $config['prev_tag_close'] = '</li>';
            $config['next_link'] = 'Next';
            $config['next_tag_open'] = '<li>';
            $config['next_tag_close'] = '</li>';
            $config['cur_tag_open'] = '<li class="current"><a href="#">';
            $config['cur_tag_close'] = '</a></li>';
            $config['num_tag_open'] = '<li>';
            $config['num_tag_close'] = '</li>';

            $config['first_tag_open'] = '<li>';
            $config['first_tag_close'] = '</li>';
            $config['last_tag_open'] = '<li>';
            $config['last_tag_close'] = '</li>';

            $config['first_link'] = 'First';
            $config['last_link'] = 'Last';

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

            $data['search'] = $this->model_products->SearchResult($config['per_page'],$this->uri->segment(2,0),$keyword);

            $this->load->vars($data);

            $this->load->view('layout/resault', $data);
            $this->load->view('layout/footer');
    }

我的模特:

function SearchResult($perPage,$uri,$keyword)
    {
        $this->db->select('*');
        $this->db->like('pro_part',$keyword);
        $this->db->or_like('pro_description',$keyword);
        $this->db->or_like('pro_name',$keyword);


        $this->db->order_by('pro_part','asc');
        $getData = $this->db->get('products', $perPage, $uri);

        return $getData->result_array();

    }


    function num_rows($keyword){

            $this->db->select('*');
            $this->db->where_in("'pro_part' like '%$keyword'");
            $this->db->or_where_in("'pro_name' like '%$keyword'");

            $query = $this->db->get('products');
            return $query->num_rows();  
        }

我的观点:

  

                                                                                                                                   特色产品&lt; / span&gt;&lt; / h2&gt; - &gt;                                     “session-&gt; userdata('word');?&gt;”的结果                                                                                                 

                      <?php if(count($search) > 0):?>
                          <?php foreach ($search as $row) :?>
                            <div class="span13 product">

                                <div>
                                    <figure>
                                        <?php if(!empty($row['pro_image'])): ?>
                                      <a href="<?= site_url("view")."?productdetail=".$row['pro_id'];?>"><img
     

src =“template / images / products /”alt =“image”/&gt;

                                          <?php else :?>
                                          <img alt="image" src="http://placehold.it/50x35"> </a>
                                          <?php endif;?>

                                    </figure>
                                    <div class="detail">
                                        <span>$<?php echo $row['pro_price'];?></span>
                                        <h4><?php echo $row['pro_part'];?></h4>
                                      <h4><?php echo $row['pro_name'];?></h4>
                                        <div class="icon">
                                            <a href="<?=site_url("add-to-cart")."/".$row['pro_id'];?>" class="one
     

工具提示“title =”添加到购物车“&gt;

                                        </div>
                                    </div>
                                </div>

                            </div>
                          <?php endforeach;?>
                          <div class="span12">
                           <div class="pagination clearfix">
                                    <!--<p>Items 1 to 9 of 12 total</p>-->
                                    <ul class="clearfix">
                                        <?php echo $this->pagination->create_links(); ?>
                                    </ul>
                                </div>
                          </div>

                          <?php else: ?>
                          <div class="span12">
                              We did not find results for"<?php echo $this->session->userdata('word') ;?>"

                              <div class="search_other">
                              <span>Other Products</span>

                              </div>
                              <div class="container">
                              <div class="row">


                              <section class="responsive slider">
<?php foreach($otherproduct as $row ): ?>   
   <div>

                                <div class="span13 product">

                                    <figure>
                                        <?php if(!empty($row->pro_image)): ?>
                                      <a href="<?= site_url("view")."?productdetail=".$row->pro_id;?>"><img
     

src =“template / images / products / pro_image;?&gt;” alt =“image”/&gt;

                                          <?php else :?>
                                          <img alt="image" src="http://placehold.it/50x35"> </a>
                                          <?php endif;?>

                                    </figure>
                                    <div class="detail">
                                        <span>$<?php echo $row->pro_price;?></span>
                                        <h4><?php echo $row->pro_part;?></h4>
                                      <h4><?php echo $row->pro_name;?></h4>
                                        <div class="icon">
                                            <a href="<?=site_url("add-to-cart")."/".$row->pro_id;?>" class="one
     

工具提示“title =”添加到购物车“&gt;

                                        </div>
                                    </div>


                              </div>

    </div>

  <?php endforeach;?>
  <?php foreach($otherproduct2 as $row ): ?>   
   <div>

                                <div class="span13 product">

                                    <figure>
                                        <?php if(!empty($row->pro_image)): ?>
                                      <a href="<?= site_url("view")."?productdetail=".$row->pro_id;?>"><img
     

src =“template / images / products / pro_image;?&gt;” alt =“image”/&gt;

                                          <?php else :?>
                                          <img alt="image" src="http://placehold.it/50x35"> </a>
                                          <?php endif;?>

                                    </figure>
                                    <div class="detail">
                                        <span>$<?php echo $row->pro_price;?></span>
                                        <h4><?php echo $row->pro_part;?></h4>
                                      <h4><?php echo $row->pro_name;?></h4>
                                        <div class="icon">
                                            <a href="<?=site_url("add-to-cart")."/".$row->pro_id;?>" class="one
     

工具提示“title =”添加到购物车“&gt;

                                        </div>
                                    </div>


                              </div>

    </div>

  <?php endforeach;?>
    <div>


  </section>



                              </div>
                              </div>
                          </div>
                      <?php endif; ?>
                        </div>

                    </div>
                </div>

我的问题是结果分页计数数据库中的所有行,不受post方法限制。

1 个答案:

答案 0 :(得分:0)

将你的模型where_in更改为where,where_in用于数组,在你的情况下将导致null,因此它不使用条件

function num_rows($keyword){

            $this->db->select('*');
            $this->db->where_in("'pro_part' like '%$keyword'");
            $this->db->or_where_in("'pro_name' like '%$keyword'");

            $query = $this->db->get('products');
            return $query->num_rows();  
        }

function num_rows($keyword){

            $this->db->select('*');
            $this->db->where("'username' like '%$keyword%'");
            $this->db->or_where("'full_name' like '%$keyword%'");

            $query = $this->db->get('cms_users');
            //$query1 = $this->db->last_query();
            //print_r($query1);
            return $query->result_array();  
        }