Codeigniter active record class join two mysql tables - php

Just want to get user_id and review_text values from table reviews, select name and city from table users where id=user_id and then return review_text, username and city to controller. Please help me.
public function did_get_reviews($item_id){
$this->db->select('user_id','review_text');
$this->db->from('reviews');
$this->db->where('post_id', $item_id);
$user_id = 'user_id' //??
$this->db->select('name','city');
$this->db->from('users');
$this->db->where('id', $user_id);
$query = $this->db->get('review_text','username','city'); //??
if ($query && $query->num_rows() >= 1){
return $query->result();
}
else {
return false;
}
}
Update 1:
I just tried to join tables but it returns the values for review_text only but I also want to get values for name and city. Please check the code below:
public function did_get_reviews($item_id){
$this->db->select('reviews.review_text','users.name','users.city');
$this->db->from('reviews','users');
$this->db->where('reviews.post_id', $item_id);
$this->db->join('users','reviews.user_id = users.user_id');
$query = $this->db->get();
if ($query && $query->num_rows() >= 1){
return $query->result();
}
else {
return false;
}
}

i think you need to use join query in SQL. if you use this code you can access to what you want
$result = $this->db->select('review, username, city')
->from('reviews')
->join('city', 'city.user_id = review.user_id')
->get();
print_r($result);
for more information about how you can write join query(left, inner or right) in codeigniter you can see this link
i hope that this code solve your problem
UPDATE
in your new question. your code have a little buggy. you need to write your select in this way
public function did_get_reviews($item_id){
$this->db->select('reviews.review_text,users.name,users.city')
->from('reviews','users')
->where('reviews.post_id', $item_id)
->join('users','reviews.user_id = users.user_id');
$query = $this->db->get();
if ($query && $query->num_rows() >= 1){
return $query->result();
}
else {
return false;
}
}
in codeigniter select Active record; any column name separate with each other by , only (not by ' and ,)
in codeigniter documentation wrote that
Permits you to write the SELECT portion of your query:
$this->db->select('title, content, date');
$query = $this->db->get('mytable');
// Produces: SELECT title, content, date FROM mytable

You can get the value for user_id using:
$this->db->select('user_id','review_text');
$this->db->from('reviews');
$this->db->where('post_id', $item_id);
$result = $this->db->get();
print_r($result);
use the same $this->db->get(); to get the values of the second query as well. Once you get the values inside a variable you can iterate it. Something like:
foreach ($result->result() as $row)
{
echo $row->name;
echo $row->city;
}

Related

Getting Data from more than two tables in Codeigniter

here is my code of model
$this->db->select('*');
$this->db->from('vf_training_district', 'vf_training_firm', 'complain_form');
$this->db->where('complain_form.InstituteId', 'vf_training_firm.FirmId');
$this->db->where('complain_form.DistrictId', 'vf_training_district.DistrictId');
$query = $this->db->get();
return $result = $query->result_array();
getting error of unknown column complain_form.InstituteId. each and every column is same as in db
Hope this helps you :
$this->db->select(*);
$this->db->from('complain_form cf');
$this->db->join('vf_training_firm vftf', 'vftf.FirmId = cf.InstituteId');
$this->db->join('vf_training_district vftd', 'vftd.DistrictId = cf.DistrictId');
$query = $this->db->get();
return $result = $query->result_array();
for more : https://www.codeigniter.com/user_guide/database/query_builder.html#selecting-data
You need JOIN:
SELECT *
FROM complain_form
JOIN vf_training_firm ON complain_form.InstituteId = vf_training_firm.FirmId
JOIN vf_training_district ON complain_form.InstituteId = vf_training_district.DistrictId
using Query Builder class Codeigniter:
$row = $this->db->select(*)
->from('complain_form')
->join('vf_training_firm', 'complain_form.InstituteId = vf_training_firm.FirmId')
->join('vf_training_district', complain_form.InstituteId = vf_training_district.DistrictId)
->get();
return $row->result_array();

How I use order by (name) in this code to get result in asc order

How I use order by (name) in this code to get result in asc order?
This is the piece of code:
function getRecordByID($id)
{
$this->db->select($this->tbl_area_menu.'.*,'.$this->tbl_users.'.firstname as created_by,'.$this->tbl_page.'.name as page_name,'.$this->tbl_page.'.slug as page_slug,'.$this->tbl_page.'.template_directory,'.$this->tbl_directory.'.slug as directory_slug');
$this->db->where($this->tbl_area_menu.'.id',$id);
$this->db->join($this->tbl_users,$this->tbl_users.'.id = '.$this->tbl_area_menu.'.created_by','left');
$this->db->join($this->tbl_page,$this->tbl_page.'.id = '.$this->tbl_area_menu.'.page_id','left');
$this->db->join($this->tbl_directory,$this->tbl_directory.'.id = '.$this->tbl_page.'.template_directory','left');
$query = $this->db->get($this->tbl_area_menu);
//echo $this->db->last_query();
$record = $query->row();
return $record;
}
Looks like you are using Query Builder. If that is the case, after all the joins add $this->db->order_by('name', 'ASC');

Displaying fields with values only with foreach limit

I have db structure
id | ref_id | amenity1 | amenity2 | amenity3 | amenity4 | amenity5
Each amenity column has values 1 or 0.
When a search is made, I need to display the field that has values not equals to zero (value != 0).
I know I can do this in VIEW as
if($data->amenity1 == 0) echo '';
But I need it to be automated with limit.
My code for MODEL is
function select_all_active_amenities($for_id){
foreach($for_id as $id){
$prop_id = $id->vbc_item_id;
}
$this->db->select('*');
$this->db->from('vbc_property_amenities');
$this->db->where_in(array('v_ref_id'=> $prop_id));
$this->db->limit(5);
$query = $this->db->get();
$result = $query->result();
return $result;
}
Please help
Add the "DISTINCT" keyword to a query before Select ,to get unique records
$this->db->distinct();
Hope it may help you out!
you just remove empty column using custom array build
function select_all_active_amenities($for_id){
foreach($for_id as $id){
$prop_id = $id->vbc_item_id;
}
$this->db->select('*');
$this->db->distinct('v_ref_id');
$this->db->from('vbc_property_amenities');
$this->db->where_in(array('v_ref_id'=> $prop_id));
// $this->db->limit(5);
$query = $this->db->get();
$result1 = $query->result_array();
//fetch the value as array
//and this query for fetch the column name what you have right now an where not equal to id and ref_id
$this->db->query("SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME='vbc_property_amenities' AND COLUMN_NAME !='id' AND COLUMN_NAME != 'ref_id AND COLUMN_NAME !='id' ")->result_array();
$query = $this->db->get();
$result2 = $query->result_array();
foreach($result as $key1=>$row1)
{
foreach($result2 as $key2=>$row2)
{
if($row2 ==$row1[$row2] && $row1 ==0)
{
unset($result[$key1][$row2]);
}
}
}
//$result only have the where value=1
return $result;
//you just limit the five in your view page
}

Codeigniter Active record Join 2 x From in statement

Hi Have written the code below which is a I think a simple select with a join.
$this->db->select('PAS_User.user_first_name, PAS_User.user_last_name, PAS_User.user_avatar_url, AMS_Notification.noti_entitiy_id, AMS_Notification.noti_entity_type, AMS_Notification.noti_updated FROM AMS_Notification INNER JOIN PAS_User ON AMS_Notification.noti_from_user_id = PAS_User.user_user_id');
$this->db->from('AMS_Notification');
$this->db->join('PAS_User', 'PAS_User ON AMS_Notification.noti_from_user_id = PAS_User.user_user_id');
$this->db->where('AMS_Notification.noti_to_user_id', $userid);
$this->db->order_by("noti_updated", "desc");
$query = $this->db->get('AMS_Notification');
if($query -> num_rows() == 1) {
return $query->result();
} else {
return false;
}
However when I call the page it shows this query with two from stamens, can any one tell me what I am doing wrong here?
SELECT `PAS_User`.`user_first_name`, `PAS_User`.`user_last_name`, `PAS_User`.`user_avatar_url`, `AMS_Notification`.`noti_entitiy_id`, `AMS_Notification`.`noti_entity_type`, `AMS_Notification`.`noti_updated` FROM AMS_Notification INNER JOIN PAS_User ON AMS_Notification.noti_from_user_id = PAS_User.user_user_id FROM (`AMS_Notification`, `AMS_Notification`) JOIN `PAS_User` ON `PAS_User` `ON` AMS_Notification.noti_from_user_id = PAS_User.user_user_id WHERE `AMS_Notification`.`noti_to_user_id` = '7' ORDER BY `noti_updated` desc
$this->db->select('PAS_User.user_first_name, PAS_User.user_last_name, PAS_User.user_avatar_url, AMS_Notification.noti_entitiy_id, AMS_Notification.noti_entity_type, AMS_Notification.noti_updated');
$this->db->from('AMS_Notification');
$this->db->join('PAS_User', 'AMS_Notification.noti_from_user_id = PAS_User.user_user_id');
$this->db->where('AMS_Notification.noti_to_user_id', $userid);
$this->db->order_by("noti_updated", "desc");
$query = $this->db->get();
if($query -> num_rows() == 1) {
return $query->result();
} else {
return false;
}
No need to specify table name and JOIN in select part

codeigniter active record get query and query without the LIMIT clause

im using active record, its all working ok but i want to set the $data["totalres"] to the total results, i mean, the same query but without the LIMIT
the problem is the previous statements gets unset when you do a query modifier, so i cant even add the $this->db->limit() after i get the results.
any ideas? i think its a bad practice to 'duplicate' the query just to do this
function get_search($start, $numrows, $filter = array())
{
...
$this->db
->select("emp")
->from('emp')
->join('empr', 'empr.b = empr.id', 'left')
->like('code', $code)
->limit($numrows, $start);
...
$q = $this->db->get();
// number of rows WITHOUT the LIMIT X,Y filter
$data["totalres"] = ???????;
if ($q->num_rows() > 0)
{
$data["results"] = $q->result();
} else {
$data["results"] = array();
}
return $data;
}
You can use SQL_CALC_FOUND_ROWS to get the number of rows that would have been returned sans-LIMIT. Note the ,FALSE in the select line. This tells CodeIgniter not to try to escape the SELECT clause with backticks (because SQL_CALC_FOUND_ROWS is not a field, and CodeIgniter doesn't realize that).
$this->db
->select("SQL_CALC_FOUND_ROWS emp", FALSE)
->from('emp')
->join('empr', 'empr.b = empr.id', 'left')
->like('code', $code)
->limit($numrows, $start);
$q = $this->db->get();
Then after that query is ran, we need run another query to get the total number of rows.
$query = $this->db->query('SELECT FOUND_ROWS() AS `Count`');
$data["totalres"] = $query->row()->Count;
Try this:
function get_search($start, $numrows, $filter = array()){
$tmp= $this->db
->select("emp")
->from('emp')
->join('empr', 'empr.b = empr.id', 'left')
->like('code', $code)
->_compile_select();
$q= $this->db->limit($numrows, $start)->get();
// number of rows WITHOUT the LIMIT X,Y filter
$data["totalres"] = $this->db->query($tmp)->num_rows();
if ($q->num_rows() > 0){
$data["results"] = $q->result();
} else {
$data["results"] = array();
}
return $data;
}
I would actually suggest the use of CIs query caching.
To use this, you start the cache, build the query without the limits in place. Run your query to get the full count, then apply the limit and run the query to get your list for your page with the offset you need.
The cache will remember the variables that have been defined.
You can then clear the cache for subsequent queries.
Example
$this->db->start_cache();
// Where, like, having clauses in here
$this->db->stop_cache();
$count = $this->db->get('table')->num_rows();
$this->db->limit('limit', 'offset');
$result = $this->db->get('table')->result_array();
$this->db->flush_cache();
$this->db
->select("SQL_CALC_FOUND_ROWS emp", FALSE)
->from('emp')
->join('empr', 'empr.b = empr.id', 'left')
->like('code', $code)
->limit($numrows, $start); $q = $this->db->get();
$query = $this->db->query('SELECT FOUND_ROWS() AS `Count`');
$data["totalres"] = $this->db->get()->row()->Count;
CodeIgniter 2.1.0 not run, below code will fix it.
$query = $this->db->query('SELECT FOUND_ROWS() AS `Count`');
$objCount = $query->result_array();
$data["totalres"] = $objCount[0]['Count'];

Categories