I have three tables named:
at_category:
at_category_taxonomy:
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. same as for remaining categories aaa, bbb, ccc etc...it may be having one or two shops else zero shops.
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
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.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.
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 . suppose my model name is Shop_model.So in controller I will write :
$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
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 */
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.