How to use LIKE in get_where - php

My Code :
public function amil_export($program){
$query = $this->get_where('tbl_amil', [ 'jenis_lembaga' => '%Perorang%',
'program' => $program
]);
$query = $this->execute();
return $query;
}
And I want to make Query in SQL like This
SELECT * FROM `tbl_amil` WHERE `jenis_lembaga` LIKE '%Perorang%' OR `program` = $program
can Someone fix my code

I've got a solution for this. I've used $this->db->group_start(); and $this->db->group_end();
public function amil_export($keyword, $program) {
$this->db->select('*');
$this->db->group_start();
$this->db->like('jenis_lembaga',$keyword);
$this->db->or_like('program',$program);
$this->db->group_end();
$query = $this->db->get('tbl_amil');
// echo $this->db->last_query();
return $query->result_array();
}
Hope this helps!!

get_where will not be able to give you the result with OR condition in it. (REFERENCE)
You can use like and or_where to get the query as you want, like so -
$query = $this->db->like('jenis_lembaga', 'Perorang')->or_where('program', $program)->get('tbl_amil')->result();
// Produces:
// SELECT * FROM `tbl_amil` WHERE `jenis_lembaga` LIKE '%Perorang%' ESCAPE '!' OR `program` = $program
You can also use query grouping (REFERENCE) as #Ankit Jindal suggests.
Hope it helps you.

public function amil_export($program)
{
$this->db->like('jenis_lembaga ','Perorang','both');
$this->db->or_where('program',$program);
$query=$this->db->get('tbl_amil');
return $query->result();
}

Related

How to make a join and search in data using codeigniter and Php

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

LIKE does not work in codeigniter

I am using LIKE to see if the similar order_id exists or not in my db. When i run the application, i see that this below code doesnt return any value. It is returning only null value.
$temp_orders = $this->transaction_model->get_similar_user_temp_transaction($cart_order_id);
function get_similar_user_temp_transaction($order_id)
{
$query = $this->db->select('*')
->from('temp_transaction')
->like('order_id', $order_id)
->get();
return ($query->num_rows() < 1) ? null : $query->result();
}
Kindly check the above code. Help would be appreciated
try your code after changing statement order
function get_similar_user_temp_transaction($order_id)
{
$query = $this->db->select('*')
->like('order_id', $order_id)
->from('temp_transaction')
->get();
return ($query->num_rows() < 1) ? null : $query->result();
}
EDIT
If you want to match exact order_id, use where instead of like
function get_similar_user_temp_transaction($order_id)
{
$query = $this->db->select('*')
->where('order_id', $order_id)
->from('temp_transaction')
->get();
return ($query->num_rows() < 1) ? null : $query->result();
}
write like in where cluase
$query = $this->db->select('*')
->from('temp_transaction')
->where("order_id LIKE '%$order_id%'")->get();
Here SQL like not work, because like is not working with INT values
You have two options :-
1. ->like(CONVERT(order_id, CHAR(16), $order_id)
2. Use ->where(order_id, $order_id)
If you are trying to match exact use where clause.
//dont need ->select('*') //excluding select gives same equivalent
//dont need from
return $this->db->where('order_id', $order_id)->get('temp_transaction')->result();

array for multiple where_in condition in codeigniter [duplicate]

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!

count the not null value using where condition in codeigniter

I am writing a function to count the null column with where condition but there is a problem in this function
protected function _get_mcq_attept_count2( $mcq_id){
$this->load->model('museranswer');
return $this->museranswe>count_by(array('mcq_id'=>$mcq_id,'bookrefrence!='=>" "));
}
this function made the query
SELECT COUNT(*) AS `numrows`
FROM `user_answer`
WHERE `mcq_id` = '321'
AND `bookrefrence` != ' '
this query return the empty column value
I hope this code work for it bcz in code-igniter i always use like this .
protected function _get_mcq_attept_count2($mcq_id)
{
$this->load->model('museranswer');
$where = array('mcq_id'=>$mcq_id);
return $this->museranswe>count_by($where);
}
/******************* FOR MODEL *********************/
public function count_by($where)
{
$this->db->select('count(mcq_id) as numrows');
$this->db->from('user_answer');
$this->db->where($where);
$this->db->where('bookrefrence !=',' ');
$qry = $this->db->get();
return $qry->result_array();
}
Change your query like this array("mcq_id" => "$mcq_id", "bookrefr‌​ence IS NOT NULL" => null). Hope you will get right answer. If it does not work, share your model with us.
return $this->museranswer->count_by(array('mcq_id'=>$mcq_id,'length(bookrefrence)>2'));

CodeIgniter where and like sql query statement

I am trying to do this sql query on codeigniter
SELECT* FROM person WHERE type = 'staff' AND description LIKE 'university%'
I am not sure if it is correct...
$this->db->get_where('person' , array('type'=>'staff'))
->like('description','university%')
->result_array();
does anyone have an idea about my case? thanks in advance ...
Using Active Record you can do so
$query = $this->db->select('*')
->from('person')
->where('type','staff')
->like('description','university','after')
->get();
$result = $query->result_array();
Make sure you pass after as a third parameter in like() function so active record will add the wild card i.e % after university so it will look like LIKE 'university%'
The docs has this explained very well, but their API is very simple. To produce the query you need the relevant code which looks like this:
$this->db->select('*')
->from('person');
->where('type', 'staff')
->like('description','university');
$query = $this->db->get();
$result = $query->result_array();
I never used chaining(even though i know it's possible), but breaking down your question should be easy ;
$this->db->from('person');
$this->db->where('type','staff');
$this->db->where('description', 'university%');
$query = $this->db->get();
$result = $query->result_array();
return $result;

Categories