【问题标题】:Codeigniter single same query with limit and without limit for pagination database with single query?Codeigniter 单个相同查询的限制和无限制的分页数据库与单个查询?
【发布时间】:2018-06-28 13:05:30
【问题描述】:

Codeigniter 单个查询有限制和无限制?

我正在尝试使用单个查询对数据库进行分页:我需要具有 where 条件且没有限制的记录总数,数据通过相同查询获得限制。

$this->db->select( '*' );
if ( ! empty( $id ) ) {
    $this->db->where( "id", $id );
}
if ( ! empty( $to ) ) {
    $this->db->where( "to", $to );
}
if ( $is_deleted !="" ) {
    $this->db->where( "is_deleted", $is_deleted );
}
if ( ! empty( $from ) ) {
    $this->db->where( "from", $from );
}
if ( ! empty( $priority ) ) {
    $this->db->where( "priority", $priority );
}
$this->db->order_by( $orderby, $order );
$total_records = $this->db->get( $this->Table )->num_rows();
if ( $page < 1 ) {
    $page = 1;
}
if ( $pagesize < 1 ) {
    $pagesize = 1;
}
$offset = ( $page - 1 ) * $pagesize;
$rows = $this->db->get( $this->Table, $pagesize, $offset )->result_array();

【问题讨论】:

    标签: php mysql codeigniter


    【解决方案1】:

    问题是您执行 querybuilder 方法 get 两次,而没有在第二个查询中定义任何内容

    在你的情况下,我更喜欢SQL_CALC_FOUND_ROWS 选项

    $this->db->select( 'SQL_CALC_FOUND_ROWS *' );
    
    $arrCheckEmptyFields = ['id' = > $id, 'to' => $to, 'from' => $from, 'priority' => $priority, 'is_deleted' => $is_deleted];
    
    foreach($arrCheckEmptyFields AS $key => $val)
    {
        if (!empty($val))
        {
            $this->db->where($key, $val);
        }
    }
    
    if ( $page < 1 ) 
    {
        $page = 1;
    }
    if ( $pagesize < 1 ) 
    {
        $pagesize = 1;
    }
    
    $offset = ( $page - 1 ) * $pagesize;
    
    
    $row = $this->db
        ->order_by( $orderby, $order)
        ->limit($pagesize, $offset)
        ->get($this->Table)
        ->result_array();
    
    $total_records = intval($this->db->query('SELECT FOUND_ROWS() AS `Count`')->row(0)->Count);
    

    【讨论】:

      【解决方案2】:
      function get_data($params = "" ){
            $this->db->start_cache();
                      $this->db->select( '*' );
                      if ( ! empty( $id ) ) {
                          $this->db->where( "id", $id );
                      }
                      if ( ! empty( $to ) ) {
                          $this->db->where( "to", $to );
                      }
                      if ( $is_deleted !="" ) {
                          $this->db->where( "is_deleted", $is_deleted );
                      }
                      if ( ! empty( $from ) ) {
                          $this->db->where( "from", $from );
                      }
                      if ( ! empty( $priority ) ) {
                          $this->db->where( "priority", $priority );
                      }
      
                      $this->db->stop_cache();
                  $this->db->order_by( $orderby, $order );
                              $total_records = $this->db->count_all_results( $this->Table );
                      if ( $page < 1 ) {
                          $page = 1;
                      }
                      if ( $pagesize < 1 ) {
                          $pagesize = 1;
                      }
                      $offset = ( $page - 1 ) * $pagesize;
                      $rows = $this->db->get( $this->Table, $pagesize, $offset )->result_array();
              $this->db->flush_cache();
          return $rows;
      }
      

      【讨论】:

        【解决方案3】:

        在 CodeIgniter 3.16 中,我需要产品总数和页面=1 个产品

        $this->db->start_cache();
        
        $this->db->select("p.*");
        $this->db->from('product p');
        
        $this->db->where("p.cat_id", "12");
        $this->db->group_by("p.id");
        
        $this->db->stop_cache();
        
        # Get total number of products
        # It will show products except limit
        $total_product = $this->db->count_all_results();
        
        
        # show only 5 products
        # It will show products including LIMIT
        $this->db->limit(5, 0);
        
        $query = $this->db->get();
        $this->db->flush_cache();
        
        
        $result       = $query->result_array();
        $result_count = $query->num_rows();  
        

        CodeIgniter Document

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-10-01
          • 2012-05-17
          • 1970-01-01
          • 2011-02-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多