![](/img/trans.png)
[英]Mysql codeigniter php asc desc order by count result using php
[英]Codeigniter + Mysql Active Record Query for ASC DESC Order
我有三个表名为:
at_category:
at_category_taxonomy:
at_shop:
我想通过计算结果来加入这三个表。
例如, at_category
表中名为“ 电子商店”的类别,其值将存储在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
当我在no.of shop列中单击asc order时,输出将是
No.Of.Shops (ASC) (Desc) Category Name (ASC) (Desc)
1 Ice cream Shops
3 Electronic Shops
这也是类别名称的副词。
现在,我想使用codeigniter通过按desc顺序计数商店数来显示结果。
在如下模型中编写方法。
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();
}
在controller中调用model方法。 假设我的模型名称是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值
这是模型代码:
<?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 */
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.