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();
Related
I have an SQL Query like this
return $this->db->query("SELECT * FROM `rincian_permintaan`
JOIN `permintaan` ON `permintaan`.`id_permintaan` = `rincian_permintaan`.`id_permintaan`
JOIN `users` ON `permintaan`.`id_peminta` = `users`.`user_id`
JOIN `cabang` ON `cabang`.`id_cabang` = `users`.`id_cabang`
JOIN `barang` ON `barang`.`id_barang` = `rincian_permintaan`.`id_barang`
JOIN `po` ON `rincian_permintaan`.`id_po` = `po`.`id_po` WHERE `po`.`id_cabang` = '201' AND `users`.`id_cabang` != 201 AND
NOT EXISTS(SELECT * FROM airwaybill WHERE airwaybill.id_rincian_permintaan = rincian_permintaan.id_rincian_permintaan)
ORDER BY rincian_permintaan.created_at DESC")->result_array();
I want to change the format to:
$this->db->select('*');
$this->db->from('tableName');
$this->db->join('...');
$this->db->where('...');
And this is what i've tried:
$this->db->select('*');
$this->db->from('rincian_permintaan');
$this->db->join('permintaan', 'permintaan.id_permintaan = rincian_permintaan.id_permintaan');
$this->db->join('users', 'permintaan.id_peminta = users.user_id');
$this->db->join('cabang', 'cabang.id_cabang = users.id_cabang');
$this->db->join('barang', 'barang.id_barang = rincian_permintaan.id_barang');
$this->db->join('po', 'rincian_permintaan.id_po = po.id_po');
$this->db->where('po.id_cabang', '201');
$this->db->where('users.id_cabang != 201');
$this->db->select('*');
$this->db->from('airwaybill');
$this->db->where('NOT EXISTS airwaybill.id_rincian_permintaan = rincian_permintaan.id_rincian_permintaan', '', FALSE);
return $this->db->get('rincian_permintaan')->result_array();
Error:
All you need is to generate sql string for subquery without executing it and use it in the outer query and produce the result.
$this->db->select('*');
$this->db->from('airwaybill');
$this->db->where('airwaybill.id_rincian_permintaan = rincian_permintaan.id_rincian_permintaan');
$sub_query = $this->db->get_compiled_select();
$this->db->select('*');
$this->db->from('rincian_permintaan');
$this->db->join('permintaan', 'permintaan.id_permintaan = rincian_permintaan.id_permintaan');
$this->db->join('users', 'permintaan.id_peminta = users.user_id');
$this->db->join('cabang', 'cabang.id_cabang = users.id_cabang');
$this->db->join('barang', 'barang.id_barang = rincian_permintaan.id_barang');
$this->db->join('po', 'rincian_permintaan.id_po = po.id_po');
$this->db->where('po.id_cabang', '201');
$this->db->where('users.id_cabang != 201 ');
$this->db->where('NOT EXISTS('.$sub_query.')');
$query = $this->db->get();
$result = $query->result_array();
return $result;
There is no result even I have data in the database:
$this->db->select('*');
$this->db->from('user');
$this->db->join('userprofile', 'user.userID = userprofile.userID');
$this->db->join('classroom', 'user.classroomID = classroom.classroomID');
$this->db->where('roleID',"4");
$query = $this->db->get();
return $query->result();
In joining you have to define the table name with where condition.Like this...
$this->db->select('*');
$this->db->from('user');
$this->db->join('userprofile', 'user.userID = userprofile.userID');
$this->db->join('classroom', 'user.classroomID = classroom.classroomID');
$this->db->where('table_name.roleID',4);//table_name is name of table having column roleID
$query = $this->db->get();
return $query->result();
$this->db->select('*');
$this->db->from('user');
$this->db->join('userprofile', 'user.userID = userprofile.userID');
$this->db->join('classroom', 'user.classroomID = classroom.classroomID');
$this->db->where('roleID',"4");
$query = $this->db->get();
return $query->result();
your query work with one table data when you join another table you can must define $this->db->where('user.roleID',4) like this.
you can write this query like this for when you get the join table data
$this->db->select('user.*,userprofile.youdesirename,classroom.class name');
$this->db->from('user');
$this->db->join('userprofile', 'user.userID = userprofile.userID');
$this->db->join('classroom', 'user.classroomID = classroom.classroomID');
$this->db->where('user.roleID',4)
$query = $this->db->get();
return $query->result();
I am trying to convert this query:
SELECT * FROM table WHERE condition1 = 'example' AND (date1 = 'date' OR renewal1 = 'renewal');
into CodeIgniter's Active Record format. I tried the following:
$q = $this->db->select('*');
$q = $this->db->from('table');
$q = $this->db->where('condition1 = condition');
$q = $this->db->where('date1 = date OR renewal = renewal');
$q = $this->db->get();
$results = $q->result();
But this did not have the desired affect. It seemed to not place the parenthesis around the second set of conditions, which caused the query to not work as expected. How else can I do this to represent what I want?
Thanks for the help!
You can use
$this->db->select('*');
$this->db->from('table');
$this->db->where('condition1 =',' condition');
$where = '(date1 = "date" OR renewal1 = "renewal")';// you can use your condition like this
$this->db->where($where);
$q = $this->db->get();
$results = $q->result();
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;
}
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'];