How to get count with joins in CodeIgniter - php

I am joining 3 tables and fetching data from database. But my problem is that I have to use 2 more tables to query to fetch a number of likes and numbers of comments on a post.
The query I am using is:
function GetHomeDeals($limit,$start)
{
$this->db->from('tbl_coupons');
$this->db->where('coupon_status', 'active');
$this->db->join('tbl_stores','tbl_stores.store_id=tbl_coupons.coupon_store');
$this->db->join('tbl_users','tbl_users.user_id=tbl_coupons.coupon_postedby');
$this->db->limit( $start,$limit);
$this->db->order_by("coupon_id", "desc");
$query = $this->db->get();
//echo $this->db->last_query();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return false;
}
}
table structure of likes tables is:
like_id
like_by
like_on
table structure for comments
comment_id
comment_by
comment_on
comment
How can i add a count of likes and comments my function?
I just want total number of likes and count in result
like_on= coupon_id
comment_on=coupon_id

use this:
$query->num_rows();
example:
$query = $this->db->get('table');
$num = $query->num_rows();

Related

Join more than 2 database table in codeigniter Error : 1066

so i kinda new with codeigniter and im trying to join 3 table from my database
database 1 : dkm (id, tgl, ref, etc)
database 2 : order_product (kode_barang, packing, nama_barang, etc)
database 3 : product (kodeprod, tglpakai, etc)
im already trying what other ppl do to join more than 2 table in codeigniter but i got this error :
Error Number: 1066
Not unique table/alias: 'order_product'
SELECT *
FROM `order_product`
JOIN `order_product` ON `order_product`.`kode_barang` = `dkm`.`id`
JOIN `order_product` ON `order_product`.`kode_barang` = `produksi`.`kodeprod`
This is my code :
Bukaka_model.php
public function getOrderProduct()
{
$this->db->select('*');
$this->db->from('order_product');
$this->db->join('order_product','order_product.kode_barang = dkm.id');
$this->db->join('order_product','order_product.kode_barang = produksi.kodeprod');
$query = $this->db->get();
return $query->result();
}
You're trying to join to the same table multiple times, instead you need to join to the other tables once each.
You just need to change the names of the table you're joining to:
public function getOrderProduct()
{
$this->db->select('*');
$this->db->from('order_product');
$this->db->join('dkm','order_product.kode_barang = dkm.id');
$this->db->join('produksi','order_product.kode_barang = produksi.kodeprod');
$query = $this->db->get();
return $query->result();
}
Try this,
Here, you have a mistake in joining tables, in CI join() in the first parameter you need to pass/write table name with you want to join
public function getOrderProduct()
{
$this->db->select('*');
$this->db->from('order_product');
$this->db->join('dkm','order_product.kode_barang = dkm.id');
$this->db->join('produksi','order_product.kode_barang = produksi.kodeprod');
$query = $this->db->get();
if($query->num_rows() > 0)
{
return $query->result();
}else{
return array();
}
}
Try this:
public function getOrderProduct()
{
$this->db->select('*');
$this->db->from('order_product');
$this->db->join('dkm','dkm.id= order_product.kode_barang');
$this->db->join('produksi',' produksi.kodeprod = order_product.kode_barang');
$query = $this->db->get();
return $query->result();
}

Inner join query in codeigniter

code:
public function draft_post($idd)
{
$this->db->select('*');
$this->db->from('registration');
$this->db->join('draft_registration', 'registration.user_id= draft_registration.user_id','INNER');
$this->db->where('registration.user_id', $idd);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
In this codes, I have two table i.e. registration and draft_registration. Now, What am I doing here I want to run inner join in Codeigniter. Now, What happening when I hit this query on phpmyadmin it shows wrong data i.e. I have two rows in draft_registration and one row in registration table but it always shows two table which is wrong and my query looks like when I was print as mention below:
SELECT *
FROM `registration`
INNER JOIN `draft_registration` ON `registration`.`user_id`= `draft_registration`.`user_id`
WHERE `registration`.`user_id` = '20181121064044'
So, How can I resolve this issue? Please help me.
Thank You
$this->db->select('*'); //This code get all rows from both table.If you want a particular row you mention the column name.
For example:
$this->db->select('registration.name,draft_registration.city,draft_registration.state');
Specify column that you want to select. Or if you want select all column of your table, you can use :
SELECT registration.* with backticks `` on column name
Use the Below Code
public function draft_post($idd)
{
$this->db->select('registration.*,draft_registration.*');
$this->db->from('registration');
$this->db->join('draft_registration', 'registration.user_id= draft_registration.user_id');
$this->db->where('registration.user_id', $idd);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
Or you can use with objects
public function draft_post($idd)
{
$this->db->select('a.*,b.*');
$this->db->from('registration a');
$this->db->join('draft_registration b', 'a.user_id= b.user_id');
$this->db->where('a.user_id', $idd);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}

Codeigniter: How to include SUM() and COUNT() in JOIN query?

I have this method in my Model
function get_users_details(){
$this->db->select("a.*,sum('b.downloads') as downloads,COUNT('b.user_email') as uploads");
$this->db->from('user a');
$this->db->join('files b', 'a.email = b.user_email','inner');
$query = $this->db->get();
if($query->num_rows() > 0)
{
foreach ($query->result_array() as $row){
$data[] = $row;
}
$query->free_result();
return $data;
}
else{
return false;
}
}
It only returns values from a single row while actually it supposed to return values from multiple rows.
sum() and count() are aggregate functions and will only return 1 row unless you combine it with a group_by statement.
SELECT count(*) FROM table_a
will return the total number of rows in table_a.
SELECT table_a.email, count(*) FROM table_a GROUP BY table_a.email
will return the total number of rows PER email address.
In codeigniter 3, we use
$this->db->group_by("table_a.email");

Fetching recent post from 3 different tables limiting the results to 5 in codeigniter

I have 3 tables named post, exam and vacancy. I need to fetch recent posts limiting the results to total 5 count. I have a sidebar widget just like wordpress has to display most recent post, but wordpress had only one table for posts.
I'm not sure how to write the query.
You can do this:
EDIT:
public function recent_posts ()
{
$this->db->select('*');
$this->db->from('post AS a, vacancy AS b, exam AS c');
$this->db->where('a.status = "Published"')->where('b.status = "Published"')->where('c.status = "Published"');
$this->db->order_by('a.post_date, b.post_date, c.post_date', 'DESC');
$this->db->limit(5);
$query = $this->db->get();
if($query->num_rows() != 0)
{
return $query->result_array();
}
else
{
return false;
}
}

Join two MySQL columns and output colB.name where colA.id is equal to colB.id

I'am using CodeIgniter active records and I have a lack in knowledge of Joining multiple table columns in MySQL.
What I basically want to do, is to output all rows from a ci_categories table, but instead of showing ci_categories.component_id as a numeric, i wish to output the ci_components.name which have equal (or same) ci_components.id from another table ci_components by using the Active Records in CodeIgniter.
What I have already done, but with errors is:
Model:
public function getItemName($id){
$this->db->select('*');
$this->db->where('id', $id);
$result = $query->result();
return $result;
}
and View:
<?php echo $this->component_model->getItemName($cat['category']->com_id);?>
Any tips pls ?
$this->db->select('name')
->from('ci_components')
->where('ci_categories.component_id = ci_componenents.id');
$query = $this->db->get();
or
$this->db->select('name');
$this->db->from('ci_components');
$this->db->join('ci_categories', 'categories.component_id = ci_components.id');
$query = $this->db->get();
https://www.codeigniter.com/userguide2/database/active_record.html#select

Categories