0

I am having problem to display correct numbers of count(field). I am trying to group by field and want to return order by group and count of them.

controller is

    $this->load->library('pagination');

    $query = "SELECT usercode,count(usercode) AS co FROM tabs
    GROUP BY usercode ORDER BY co desc";

    $config['total_rows'] = $this->db->query($query)->num_rows();
    $config['num_links'] = '25';
    $config['uri_segment'] = 3;
    $config['base_url'] = base_url() . "/user/topCreators/";
    $config['per_page'] = '25';
    $config['anchor_class'] = " class=\"number\" ";
    $config['cur_tag_open'] = "<a href=\"#\" class=\"number 
    current\" title=\"Current Page\">";
    $config['cur_tag_close'] = "</a>";

    $this->pagination->initialize($config);
    if ($this->uri->segment(3) == FALSE){
        $offset = 0;
    }
    else
    {
        $offset = $this->uri->segment(4);
    }

    $limit = $config['per_page'];
    $data["total_records"] = $config['total_rows'];
    $data["page_links"] = $config["per_page"];


    $data["query"] = $this->db->query($query . " LIMIT $limit OFFSET $offset");

    $this->load->view("top_creators", $data);

my view file is

   <?php foreach($query->result() as $me) {?>
   <?= $me->co?>  

   <?php }?>
4

2 回答 2

1

co每个结果的数字都是相同的,因为您正在计算表格选项卡中的所有用户代码,因此您的订购将不起作用。它也使执行变得多余,$this->db->query($query)->num_rows();因为它的值与co

于 2012-09-01T21:28:29.093 回答
0

您应该更详细地解释在您的情况下什么不起作用。


在我看来,处理分页的最佳做法是使用SQL_CALC_FOUND_ROWS

基本上,这使您可以运行一个查询并通过

// Main query with SQL_CALC_FOUND_ROWS

$row = $this->db->query("SELECT FOUND_ROWS() AS found")->row();
$config['total_rows'] = $row->found;

SQL_CALC_FOUND_ROWS计算所有行,但仅返回LIMIT内的行。这可以让您对系统进行更少的查询(更少的延迟),并且总是会给出正确的 total_rows,因为您从同一查询中获取值。

于 2012-09-01T22:50:22.250 回答