I am using codeigniter active record query.
function select_in($table,$cond)
{
$this->db->select('*');
$this->db->from($table);
$this->db->where_in('brand_id',$cond);
$query = $this->db->get();
//echo $this->db->last_query(); exit;
return $query;
}
I need to pass another one where_in condition in this query.Is it Possible?
Try with -
$this->db->where_in('brand_id',$cond);
$this->db->where_in('field' , $cond_new);
Related
I have two join tables; parent and student. I have to update both tables from one button click. For that, I want to write a function "get data by id". I managed to write that code only for one table.
How do you write the following code if I want to get data from two tables? if p_id (parent id) is the foreign key?
Model
function get_by_id($id)
{
$this->db->from('student');
$this->db->where('p_id',$id);
$query = $this->db->get();
return $query->row();
}
Controller
public function ajax_edit($id)
{
$data = $this->Model_Action->get_by_id($id);
echo json_encode($data);
}
Hi I think you are looking for this. I use a sample from your code:
function get_by_id($id)
{
$this->db->from('student');
$this->db->join('table_b', 'student.p_id=table_b.p_id');
$this->db->where('student.p_id',$id);
$query = $this->db->get();
return $query->row();
}
Actually you can find more here
function get_by_id($id)
{
$this->db->select('*')
$this->db->from('student');
$this->db->join('parent','student.p_id=parent.p_id');
$this->db->where('student.p_id',$id);
$query = $this->db->get();
return $query->row();
}
I make a join between two tables using codeigniter framework and this is my query:
public function SearchDataUnderCondition($firsttable,$secondtable,$data)
{
$this->db->select("atm.* , tender.status as tenderstatus");
$this->db->from($firsttable);
$this->db->join($secondtable,'atm.id_tender=tender.id');
$this->db->where('tenderstatus','1');
$this->db->like('serial', $data);
$sql = $this->db->get();
return $sql->result();
}
I am using database model when is remove
$this->db->where('tenderstatus','1');
from my code operation done and i get result but I want to make search under this condition. what is my problem?
Try this
public function SearchDataUnderCondition($data)
{
$this->db->select('atm.* , tender.status');
$this->db->from('atm');
$this->db->join('tender','atm.id_tender=tender.id');
$this->db->where('tender.status','1');
$this->db->like('serial', $data);
$sql = $this->db->get();
return $sql->result();
}
Try this
$this->db->where('tender.status','1');
I think, we can not aliases in where clause
Using aliases (and also making sure you want and WHERE AND LIKE clause):
$fist_table = 'atm';
$second_table = 'tender';
$this->db->select('aliasone.* , aliastwo.status as "tenderstatus"');
$this->db->from("$first_table as aliasone");
$this->db->join("$second_table as aliastwo", "aliastwo.id = aliasone.tender_id");
$this->db->where('aliastwo.status','1');
$this->db->like('serial', $data);
return $this->db->get();
This question already has an answer here:
Codeigniter model with multiple update conditions using manual where statement
(1 answer)
Closed 5 years ago.
$this->db->where('column_field1',$value1);
$this->db->where('column_field2',$value2);
For above two query we can write a single query as:
$arr = array('column_field1'=>'value1', 'columne_field2'=>'value2');
function select($arr){
.. .. ..
... .. . ..
$this->db->where($arr);
}
is there any solution for the where_in query:
function($value1, $value2){
$this->db->where_in('column_field1',$value1);
$this->db->where_in('column_field2',$value2);
}
as I have tried but didn't work:
arr = array('column_field1'=>$arr,'column_field2'=>arr2);
function select_in($arr)
{
$this->db->select('*');
$this->db->from('table');
$this->db->where_in($arr);
$query = $this->db->get();
return $query;
}
I want to combine the where_in condition so that i can store multiple column_field and array for it.
$this->db->where_in($arr);
where $arr contains pair of column_filed and array_of_value:
$arr = $array('column_field'=>$arr_of_value);
function select_in($arr)
{
$this->db->select('*');
$this->db->from('table');
$this->db->where($arr); // change here
$query = $this->db->get();
return $query;
}
If you want multiple where In then you need to write it twice....It's not possible in single statement.
$this->db->where_in('field1',$cond1);
$this->db->where_in('field2' , $cond2);
Note: Where_in is similar to where id IN (1,2,3...)but in your case you are doing multiple where condition.
Creating custom method is a reasonable solution for this, you can either extend database's active record class or write custom function which takes array and call where_in like below
function multiple_where_in($array){
foreach($array as $key => $data){
$this->db->where_in($key, $data);
}
}
And call like below
function select_in($arr)
{
$this->db->select('*');
$this->db->from('your_table');
$this->multiple_where_in($arr); // call local function created above
$query = $this->db->get();
return $query;
}
// create array like below, fieldname and field_values inside array
$arr = array(
'column_field1'=>array('value1','value2'),
'columne_field2'=>array('value2','value3')
);
// call your select method
$this->select_in($arr);
You can try this solution for your problem.
$this->db->select('*');
$this->db->from('table');
if(!empty($cond1) && !empty($cond2)){
$this->db->where("field1 IN (".$cond1.") AND field2 IN (".$cond2.") ",null,false);
}
$query = $this->db->get();
return $query;
I hope this will helps you. Thanks!
here's my model code :
function total($id)
{
$this->db->select_sum('score');
$q = $this->db->get('my_table');
$this->db->where('id',$id);
$this->db->group_by('id');
return $q->row()->score;
}
why the output still sum all of row not the specific row with id?
$this->db->get() actually runs the query. You need to call that last.
function total($id)
{
$this->db->select_sum('score');
$this->db->where('id',$id);
$this->db->group_by('id');
$q = $this->db->get('my_table');
return $q->row()->score;
}
I am working in codeigniter.. I want to count the no. of rows having the same order_no.. below is my code.
public function get_my_orders() {
$user_data = $this->session->all_userdata();
$email = $user_data['user_email'];
$this->db->select('*');
$this->db->from('order_details');
$this->db->where('email', $email);
$this->db->group_by('order_no');
$this->db->order_by('order_no', 'DESC');
$query = $this->db->get();
return $query->result();
}
Pls help..
You can use $this->db->count_all_results()
public function get_my_orders() {
$user_data = $this->session->all_userdata();
$email = $user_data['user_email'];
//$this->db->select('*');// no need select as you only want counts
$this->db->from('order_details');
$this->db->where('email', $email);
//$this->db->group_by('order_no');//no need group_by as you only want counts
//$this->db->order_by('order_no', 'DESC');//no need order_by as you only want counts
return $this->db->count_all_results();;
}
Try changing your select line to look like this:
$this->db->select('COUNT(*) as count');
Rather than having all fields accessible like they currently are, you will instead only have access to one variable called count. To keep all variables accessible and add the count in as well, use this instead:
$this->db->select('*, COUNT(*) as count');
Feel free to change the lowercase name for count, I'm just using that as an example.
Check Codeigniter Manual
$query = $this->db->get();
return $query->num_rows(); //Return total no. of rows here
$query->num_rows(); will count the total active record return by your query.