【问题标题】:Codeigniter + Mysql Active Record Query for ASC DESC OrderCodeigniter + Mysql Active Record 查询 ASC DESC 订单
【发布时间】:2014-01-24 08:49:51
【问题描述】:

我有三个表名为:

at_category:

  1. cat_id
  2. 姓名

at_category_taxonomy:

  1. cat_taxonomy_id
  2. cat_id
  3. 分类法

at_shop:

  1. shop_id
  2. shop_category

我想通过计算结果来加入这三个表。

例如,at_category 表中名为 Electronic Shops 的类别和值将存储在 at_category_taxonomy 表中,并且此类别 ID 在 at_shop 表中有两个商店。与其余类别 aaa、bbb、ccc 等相同......它可能有一个或两个商店,否则为零商店。

例子:

1. at_category

    ______________

    cat_id   name

     1       Electronic Shops
     2       Ice Cream Shops
    _______________

    2. at_category_taxonomy

    _______________________________________________

    cat_taxonomy_id   cat_id   taxonomy

      3                 1       Electronic Shops
      4                 2       Ice Cream Shops
    _______________________________________________

    3. at_shop

    ________________________________

    shop_id   shop_name   shop_category

     1            A         1 (ie.Electronic Shops) 
     2            B         1 (ie.Electronic Shops) 
     3            C         1 (ie.Electronic Shops) 
     4            D         2 (ie.Ice Cream Shops) 

    ________________________________

现在:电子商店有 3 家商店和冰淇淋店有 1 家商店的类别

预期输出:

No.Of.Shops (ASC) (Desc)    Category Name   (ASC) (Desc)

     3                         Electronic Shops
     1                         Ice cream Shops 

当我在商店数量列中单击 asc order 时,输出将是

No.Of.Shops (ASC) (Desc)    Category Name   (ASC) (Desc)

     1                          Ice cream Shops   
     3                          Electronic Shops

这也是类别名称的反义词。

现在我想通过使用 codeigniter 按 desc 顺序计算商店数量来显示结果。

【问题讨论】:

标签: php mysql codeigniter


【解决方案1】:

在模型中编写一个方法,如下所示。

public function get_shops_by_category($order_by_column='shop_cnt',$order_by_method='DESC'){ 
    $this->db->select('c.name,COUNT(s.shop_id) as shop_cnt');
    $this->db->from('at_category AS c');
    $this->db->join('at_shop AS s','c.cat_id = s.shop_category','left');
    $this->db->group_by("c.cat_id"); 
    $this->db->order_by("{$order_by_column}", "{$order_by_method}"); 
    return $this->db->get()->result();
}

在控制器中调用模型方法。假设我的模型名称是 Shop_model。所以在控制器中我会写:

$order_by_method = (!empty($this->input->get($order_by_method))) ? $order_by_method : 'DESC';
$order_by_column = (!empty($this->input->get($order_by_column))) ? $order_by_column : 'shop_cnt';

$result = $this->shop_model->get_shops_by_category($order_by_column,$order_by_method);

在 clikc 上,您需要在 url 中传递 order_by_column 值和 order_by_method 值

【讨论】:

  • 你检查了吗?我只是通过创建控制器和模型尝试了一切,当我打印结果数组时,我得到了以下结果: Array ( [0] => stdClass Object ( [name] => Electronic Shops [shop_cnt] => 3 ) [1] => stdClass Object ( [name] => Ice Cream Shops [shop_cnt] => 1 ) )
【解决方案2】:

这是型号代码:

<?php //if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Shop_model extends CI_Model {

    public function __construct(){
        parent::__construct();  
    }

    public function get_shops_by_category($order_by_column='shop_cnt',$order_by_method='DESC'){ 
        $this->db->select('c.name,COUNT(s.shop_id) as shop_cnt');
        $this->db->from('at_category AS c');
        $this->db->join('at_shop AS s','c.cat_id = s.shop_category','left');
        $this->db->group_by("c.cat_id"); 
        $this->db->order_by("{$order_by_column}", "{$order_by_method}"); 
        return $this->db->get()->result();
    } 

}

下面是控制器:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Shop extends CI_Controller {

    public function __construct(){
        parent::__construct();
    }

    public function index(){
        $this->load->model('Shop_model');

        $order_by_method = $this->input->get('order_by_method', TRUE);
        $order_by_column = $this->input->get('order_by_column', TRUE);  
        if(empty($order_by_method)){
            $order_by_method = 'DESC';
        }
        if(empty($order_by_column)){
            $order_by_column = 'shop_cnt';
        }           

        $result = $this->Shop_model->get_shops_by_category($order_by_column,$order_by_method);
        echo '<pre>';
        print_r($result);
        //$this->output->enable_profiler(TRUE);
        //$this->load->view('welcome_message');
    }
}

/* End of file Shop.php */
/* Location: ./application/controllers/Shop.php */

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-30
    • 2012-04-17
    • 2013-02-25
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    • 2016-03-15
    • 2013-01-09
    相关资源
    最近更新 更多