CodeIgniter Active Record multiple WHERE clauses - php

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();

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 to select data between two dates using 'AND','OR' clause?

i am trying to fetch data from database table in between two dates and it is having two more filter using 'OR' clause but in result i am getting data either between date or data fetched using 'OR' clause.I am posting here query in model if anyone knows solution please let me know.
Thanks in advance
$this->db->select('*');
$this->db->from('Transaction');
$this->db->join('Users', 'Transaction.user_id = Users.id');
$this->db->where('date >=',$startDate);
$this->db->where('date <=',$endDate);
$this->db->where( 'against = Recharge');
$this->db->or_where('against =','Rech_Commission');
$query = $this->db->get();
return $query->result();
I want date filter compulsory but want to fetch records against=Recharge or against='Rech_commission'but i am getting either using date range or against='Rech_Commission'
use
$where = '(against="Recharge" or against = "Rech_Commission")';
$this->db->where($where);
and you had date and date and (against = '' or against = '')
$this->db->select('*');
$this->db->from('Transaction');
$this->db->join('Users', 'Transaction.user_id = Users.id');
$this->db->where('date BETWEEN "'. date('Y-m-d', strtotime($startDate)). '" and "'. date('Y-m-d', strtotime($endDate)).'"');
$this->db->where('against="Recharge" OR against = "Rech_Commission")';
$query = $this->db->get();
return $query->result();
$this->db->select('*');
$this->db->from('Transaction');
$this->db->join('Users', 'Transaction.user_id = Users.id');
$this->db->group_start();
$this->db->where('date >=',$startDate);
$this->db->where('date <=',$endDate);
$this->db->where( 'against = Recharge');
$this->db->group_end();
$this->db->or_where('against =','Rech_Commission');
$query = $this->db->get();
return $query->result();

No result in Joining 3 tables in Codeigniter

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();

Combined conditions in CI ActiveRecord

I want to do a sql query like
select * where x = 10 and (y=12 or h=15)
How can I achieve that in CI ActiveRecord format?
See Active record reference
$where="x = 10 and (y=12 or h=15)";
$this->db->select('*');
$this->db->from('mytable');
$this->db->where($where);
$query = $this->db->get();
Try like
$sql = "SELECT * FROM TABLE_NAME WHERE x = 10 AND (y = 12 OR h = 15)";
$result = $this->db->query($sql);
Or like
$this->db->select('*');
$this->db->from('TABLE_NAME');
$this->db->where('x',10);
$this->db->AND('y',12);
$this->db->or_where('h',15);
$query = $this->db->get();

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