Codeigniter单一查询具有限制,对单个查询的分页数据库没有限制?

时间:2018-01-19 10:07:55

标签: php mysql codeigniter

Codeigniter单一查询有限制但没有限制?

我尝试用单一查询分页数据库:我需要记录的总数,其中条件无限制,数据与相同查询一起获得限制。

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

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

问题是你执行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 :(得分:0)

在CodeIgniter 3.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 Document