php - 代码点火器 : search pagination not limit by post method?

标签 php codeigniter

我的 Controller :

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

我的看法:

Featured Products </span></h2>--> Results for "session->userdata('word') ;?>"

                      <?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" />

                                          <?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

tooltip" title="Add to cart">

                                        </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 ;?>" alt="image" />

                                          <?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

tooltip" title="Add to cart">

                                        </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 ;?>" alt="image" />

                                          <?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

tooltip" title="Add to cart">

                                        </div>
                                    </div>


                              </div>

    </div>

  <?php endforeach;?>
    <div>


  </section>



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

                    </div>
                </div>

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

最佳答案

将您的模型 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();  
        }

关于php - 代码点火器 : search pagination not limit by post method?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39345222/

相关文章:

php - 如何在 CodeIgniter 中压缩图像,但不改变它们的尺寸?

java - 我可以在php中使用base64解码用java编码的数据吗?

CodeIgniter 模型/ Controller 和用户 ID

php - Codeigniter 缓存文件不会被删除

javascript - 类型错误 : 'append' called on an object that does not implement interface FormData

php - 按类别和标签列出 WordPress 帖子

php - 使用php exec函数进行Powershell远程执行

php - 这是使用 PHP CURL 发出 API 请求的最佳方式吗?

php - Codeigniter mysql 查询使用 UNION 首先选择等于今天日期的值

codeigniter - 加拿大邮政从加拿大到美国的运输方式