How to include multiple condition on get_where in codeigniter? - codeigniter

Im trying to include to conditions on my get_where but its not working. Single condition works but I need two conditions. Any idea?
Here's my code:
$query = $this->db->get_where($this::DB_TABLE, array("$column = " => $id, " $column2 = " => $id2, ));

As i see you are trying to combine the Active Record with Query, so the best way to declare conditions is:
$this->db->select('*');
$this->db->from('table');
$this->db->where('table.column', $condition1);
$this->db->where('table.column2', $condition2);
$this->db->where('table.column3', $condition3);
$q = $this->db->get();
foreach($q->result() as $row){
var_dump($row);
}
or
$this->db->select('*');
$this->db->from('table');
$this->db->where(array('table.column' => $condition1, 'table.column2' => $condition2, 'table.column3' => $condition3));
$q = $this->db->get();
foreach($q->result() as $row){
var_dump($row);
}

$where_array = array(
$column =>$id,
$column2=>$id2
);
$query = $this->db->get_where($this::DB_TABLE,$where_array);

instead, I just did this:
$query = "select * from ".$this::DB_TABLE." name where $column=? and $column2 = ?";
$sql = $this->db->query($query, array($id,$id2));
Thanks, Shaiful Islam :)

I have found one thing with where condition. May be helpful for you.
$this->db->group_start();
$this->db->group_end();

Try this code
$query = $this->db->get_where('table1,table2', array('table1.$id' => $id,'table2.condition' => $condition));
return $query->result_array();

Related

Laravel query groubBy condition

I have a dB query where I would like to groupBy() only when conditions are met without using union because of pagination.
Unfortunately groupBy() seems to only work when called on the entire query outside of the loop.
This was made for dynamic filtering from $filterArr. Depending on the array I need to select from different columns of the table.
When the $key=='pattern' I would need the distinct results from its column.
the query looks something like this
select `col_1`, `col_2`, `col_3`
from `mytable`
where (`color` LIKE ? or `pattern` LIKE ? or `style` LIKE ?)
group by `col_2` //<< i need this only for 'pattern' above and not the entire query
Heres the model:
// $filterArr example
// Array ( [color] => grey [pattern] => stripe )
$query = DB::table('mytable');
$query = $query->select(array('col_1', 'col_2', 'col_3'), DB::raw('count(*) as total'));
$query = $query->where(function($query) use ($filterArr){
$ii = 0;
foreach ($filterArr as $key => $value) {
if ($key=='color'){
$column = 'color';
}else if ($key=='style'){
$column = 'style';
}else if ($key=='pattern'){
$column = 'pattern';
$query = $query->groupBy('col_2'); // << !! does not work
}
if($ii==0){
$query = $query->where($column, 'LIKE', '%'.$value.'%');
}
else{
$query = $query->orWhere($column, 'LIKE', '%'.$value.'%');
}
$ii++;
}
});
$query = $query->orderBy('col_2', 'asc')->simplePaginate(30);
I think you can simplify your code a bit:
$query = DB::table('mytable');
$query = $query->select(array('col_1', 'col_2', 'col_3'), DB::raw('count(*) as total'));
$query = $query->where(
collect($filterArr)
->only(['color','style','pattern'])
->map(function ($value, $key) {
return [ $key, 'like', '%'.$value.'%', 'OR' ];
})->all()
)->when(array_key_exists('pattern', $filterArr), function ($query) {
return $query->groupBy('col_2');
});
$query = $query->orderBy('col_2', 'asc')->simplePaginate(30);

Codeigniter db_where in 3 joined tables

So this code is a combination of 3 table joined, now i want to filter the query where only the request that a user can have is his/her own request and not from other user.. the problem is in where clause i have duplicated.
$this->db->order_by('loanrequest.ApplicationNo', 'DESC');
$query = $this->db->get('loanrequest','loanapplication','user');
$this->db->join('loanapplication','loanrequest.ApplicationNo = loanapplication.ApplicationNo');
$this->db->join('user', 'user.userId = loanapplication.userId');
$this->db->where('loanapplication.userId', $this->session->userdata('userId'));
$this->db->where('loanapplication.ApplicationNo', 'loanrequest.ApplicationNo');
return $query->result_array();
I can not get what you have done in your join query but Here is how I do join in my CodeIgniter model.
You don't need to put this in where condition:
$this->db->where('loanapplication.ApplicationNo', 'loanrequest.ApplicationNo');
Because you have already done that in the join query.
$query = $this->db->select('*')
->from('loanapplication')
->join('loanrequest', 'loanrequest.ApplicationNo = loanapplication.ApplicationNo', 'left')
->join('user', 'user.userId = loanapplication.userId', 'left')
->where_in('loanapplication.userId',$this->session->userdata('userId'))
->order_by('loanrequest.ApplicationNo', 'DESC')
->get();
return $query->result_array();
I will recommend you should use $this->db->last_query() because it gives you a better idea about your query.
$query = $this->db->select('*')
->from('loanapplication')
->join('loanrequest', 'loanrequest.ApplicationNo = loanapplication.ApplicationNo', 'left')
->join('user', 'user.userId = loanapplication.userId', 'left')
->where_in('loanapplication.userId',$this->session->userdata('userId'))
->order_by('loanrequest.ApplicationNo', 'DESC');
$result = $query->get();
echo $this->db->last_query();exit; //use this to print your query so that you can get the actual issue
if ($result->num_rows() > 0) {
return $result->result_array();
} else
return '';

how to fetch all records from order table in Codeigniter

$data is an array which contain user post data for fetch record from orders table
$data=array('customer_id'=>$this->input->post('custId'),'paided'=>2);
$this->db->select('*');
$this->db->from('orders');
$this->db->where($data);
$this->db->get();
$data = array(
'customer_id' => $this->input->post('custId')],
'paided' => 2
);
$this->db->select('*');
$this->db->from('orders');
$this->db->where($data);
$this->db->get();
try this :
public function function_name (){
$data = array (
'customer_id' => $this->input->post('custId'),
'paided' => 2
);
$this->db->select('*');
$this->db->from('ordere');
$this->db->where($data);
$query = $this->db->get();
return $query->result_array();
}
You have done all good just need to put result() if you get multiple row or row() if you get one row
$data=array('customer_id'=>$this->input->post('custId'),'paided'=>2);
$this->db->select('*');
$this->db->from('orders');
$this->db->where($data);
$result= $this->db->get()->result(); //added result()
print_r($result);
as simple use
$custId = $_post['custId'];
$query = $this->db->query("SELECT * FROM orders WHERE customer_id= '$custId' AND paided='2'");
$result = $query->result_array();
return $result;//result will be array
This is the plus of using framework, you don't need to write that much of code,
$where = array('customer_id' => $this->input->post('custId'),'paided'=>2)
$result = $this->db->get_where('orders', $where);
and for fetching them, use $result->row() for single record retrieval.
If you want all records, use $result->result()
Here is documentation link, if you want to learn more.
What You should need to be Correct And Why
$data=array('customer_id'=>$this->input->post('custId'),'paided'=>2);
$this->db->select('*'); // by defaul select all so no need to pass *
$this->db->from('orders');
$this->db->where($data);
$this->db->get(); // this will not return data this is just return object
So Your Code Should be
$data=array('customer_id'=>$this->input->post('custId'),'paided'=>2);
$this->db->select(); // by defaul select all so no need to pass *
$this->db->from('orders');
$this->db->where($data);
$query = $this->db->get();
$data = $query->result_array();
// or You can
$data= $this->db->get()->result_array();
here result_array() return pure array where you can also use result()
this will return array of object

codeigniter search filter model

I have created a search filter with my pagination model, When I type in my filter it does not pick up if I just have one letter for some reason my model requires that I have min 2 letters to start filtering
What is the best method in model so my search field can pick up just one letter typed in the filter input.
public function getUserGroups($limit = null, $offset = NULL) {
$this->db->select("user_group_id, name");
$this->db->from($this->db->dbprefix . 'user_group');
$this->db->limit($limit, $offset);
$this->db->order_by('name', 'asc');
$data = array(
'name' => $this->input->post('name'),
'user_group_id' => $this->input->post('user_group_id')
);
$this->db->like($data);
$this->db->or_like('name', 'match');
$this->db->or_like('user_group_id', 'match');
$query = $this->db->get();
return $query->result_array();
}
I think following query will solve your problem
public function getUserGroups($limit = null, $offset = NULL) //better use $limit=0,$offset=0
{
$this->db->select("user_group_id, name");
$this->db->from($this->db->dbprefix . 'user_group');
$this->db->limit($limit, $offset);
$this->db->order_by('name', 'asc');
$data = array(
'name' => $this->input->post('name'),
'user_group_id' => $this->input->post('user_group_id')
);
$this->db->or_like($data);
$query = $this->db->get();
return $query->result_array();
}
Remember
They way you using like and or_like may produce wrong query.Make sure your query is producing right way as you wanted

How can use order-by in codeigniter?

I know this is simple but i didn't complete it.
$query = $this->db->get_where('prepared_forms', array('topic' => $this->input- >post('prepared_topics')));
$new_form = $query->row_array();
How can order prepared forms order by topic name (ASC)?
$this->db->select("*")
->from('prepared_forms')
->where('topic', $this->input->post('prepared_topics'))
->order_by('topic', 'asc')
->get()
->result_array();
Try this:
$query = $this->db->order_by('topic', 'asc')->get_where('prepared_forms', array('topic' => $this->input->post('prepared_topics')));
$new_form = $query->row_array();
$this->db->order_by("topic", "asc");
$query = $this->db->get_where('prepared_forms', array('topic' => $this->input->post('prepared_topics')));
$new_form = $query->row_array();

Resources