[英]Codeigniter + Mysql Active Record Query for ASC DESC Order
I have three tables named: 我有三个表名为:
at_category: at_category:
at_category_taxonomy: at_category_taxonomy:
at_shop: at_shop:
I want to join those three tables by counting result. 我想通过计算结果来加入这三个表。
For example the category named as Electronic Shops in at_category
table and the value will be stored in at_category_taxonomy
table and the this category id's are having two shops in at_shop
table. 例如,
at_category
表中名为“ 电子商店”的类别,其值将存储在at_category_taxonomy
表中,并且此类别ID在at_shop
表中具有两个商店。 same as for remaining categories aaa, bbb, ccc etc...it may be having one or two shops else zero shops. 与其余类别aaa,bbb,ccc等相同...它可能拥有一两个商店,其他商店为零。
Example : 范例:
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)
________________________________
Now : The category Electronic Shops having 3 shops and Ice cream shops having 1 shop 现在:类别拥有3家商店的电子商店和拥有1家商店的冰淇淋商店
Expecting output : 预期产量:
No.Of.Shops (ASC) (Desc) Category Name (ASC) (Desc)
3 Electronic Shops
1 Ice cream Shops
When i click asc order in no.of shops column then the output will be 当我在no.of shop列中单击asc order时,输出将是
No.Of.Shops (ASC) (Desc) Category Name (ASC) (Desc)
1 Ice cream Shops
3 Electronic Shops
This is vise verse for category name too. 这也是类别名称的副词。
now I want to display the result by count the number shops by desc order using codeigniter. 现在,我想使用codeigniter通过按desc顺序计数商店数来显示结果。
Write a method in a model like below. 在如下模型中编写方法。
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();
}
Call the model method in controller . 在controller中调用model方法。 suppose my model name is Shop_model.So in controller I will write :
假设我的模型名称是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);
on clikc you need to pass order_by_column value and order_by_method value in url 在clikc上,您需要在url中传递order_by_column值和order_by_method值
This is the model code: 这是模型代码:
<?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();
}
}
Below is the controller: 下面是控制器:
<?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 */
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.