用于ASC DESC命令的Codeigniter + Mysql活动记录查询

时间:2014-01-24 08:49:51

标签: php mysql codeigniter

我有三个名为:

的表

at_category:

  1. CAT_ID
  2. 名称
  3. at_category_taxonomy:

    1. cat_taxonomy_id
    2. CAT_ID
    3. 分类
    4. at_shop:

      1. shop_id
      2. shop_category
      3. 我想通过计算结果来加入这三个表。

        例如at_category表中名为电子商店的类别,该值将存储在at_category_taxonomy表中,此类别ID在{{{{}}中有两个商店1}}表。与剩余的类别 aaa,bbb,ccc 等相同......它可能有一两个商店,零店铺。

        示例:

        at_shop

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

        期待输出:

        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) 
        
            ________________________________
        

        当我点击no.of商店列中的asc订单时,输出将是

        No.Of.Shops (ASC) (Desc)    Category Name   (ASC) (Desc)
        
             3                         Electronic Shops
             1                         Ice cream Shops 
        

        这也是类别名称的vise verse。

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

2 个答案:

答案 0 :(得分:0)

在下面的模型中编写方法。

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.So在控制器中我会写:

$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值

答案 1 :(得分:0)

这是型号代码:

<?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 */