codeigniter单个相同查询有限制和无限制分页数据库有单一查询?

yqhsw0fo  于 2021-06-21  发布在  Mysql
关注(0)|答案(3)|浏览(307)

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();
70gysomp

70gysomp1#

在codeigniter3.16中,我需要产品的总数&page=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文档

3j86kqsm

3j86kqsm2#

问题是执行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);
n1bvdmb6

n1bvdmb63#

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

相关问题