Like query string in codelgniter - codeigniter

My view page has the following fields
id,name,email,gender etc..
I have a search box , and i want to perform search in all the above fields
How to write the query string in CI way
Controller
public function search()
{
$sql = "SELECT * FROM users";
$this->form_validation->set_rules('search_term', 'Search_term', 'trim|xss_clean');
if ($this->form_validation->run() == TRUE)
{
$keyword = $this->input->post('search_term');
$sql.= "WHERE name like '{$keyword }'";
$sql.= "OR email like '{$keyword }'";
$sql.= "OR gender like '{$keyword }'";
}
$query = mysql_query($sql);
}

read CI Like like query
$keyword = $this->input->post('search_term');
$this->db->select('*');
$this->db->like('name', $keyword);
$this->db->or_like('email', $keyword);
$this->db->or_like('gender', $keyword);
$res = $this->db->get('users');

Related

Laravel Yajra datatable global Search filter

I have this technical difficulty when want to use laravel yajra datatable to write custom global search query
Reference
https://yajrabox.com/docs/laravel-datatables/master/filter-column
when I do some search by passing in calendar date and searchbox value, I'm stuck at correct way to write it
Only the main ->filter got triggered
The ->filterColumn skipped through.... not sure why...
In sql vanila, it is
where firstname like %search% OR
lastname like %search% OR
fullname like %search% OR
email like %search% OR
sponsorby like %search% OR
createddate like %date%
Here is my code for reference. filterCustom is not triggered...
if ($request->ajax()) {
$users = \DB::select('
SELECT id, usercode as membercode, concat(firstname, " ", lastname) as fullname, email, created_at as createddate, (select concat(firstname, " ", lastname) from users where id= m.parentid) sponsorby
FROM users m
ORDER BY m.created_at ASC');
Update: I realize yajra doesn't support DB::select with full raw sql, however it support DB::table
So I change my sql to below,
$users = \DB::table('users')
->select('id', 'usercode as membercode', \DB::raw('concat(firstname, " ", lastname) as fullname'), 'email', 'created_at as createddate', \DB::raw('(select concat(firstname, " ", lastname) from users as b where b.id= users.parentid) as sponsorby'));
//end
return Datatables::of($users)
->addColumn('membercode', function($row){
$content= $row->membercode;
return $content;
})
->addColumn('fullname', function($row){
$content= $row->fullname;
return $content;
})
->addColumn('email', function($row){
$content= $row->email;
return $content;
})
//etc
->filter(function ($sql) use ($request) {
$search = $request->search;
if (!empty($search))
{
$sql->Where('email', 'LIKE', "%$search%");
}
})
->filterColumn('fullname', function($query, $request) {
$search = $request->search;
$sql = 'concat(firstname, " ", lastname) like ?';
$query->whereRaw($sql, ["%{$search}%"]);
})
->filterColumn('membercode', function($query, $request) {
$search = $request->search;
$sql = 'usercode as membercode like ?';
$query->whereRaw($sql, ["%{$search}%"]);
})
->filterColumn('createddate', function($query, $request) {
$search = $request->date;
$sql = 'created_at as createddate like ?';
$query->whereRaw($sql, ["%{$search}%"]);
})
->filterColumn('sponsorby', function($query, $request) {
$search = $request->search;
$sql = '(select concat(firstname, " ", lastname) from users as b where b.id= users.parentid) like ?';
$query->whereRaw($sql, ["%{$search}%"]);
})
->make(true);
}

Laravel combine query with if statement

Im working on product filtering using AJAX. Is there any possible way to produce same output as picture shown below using query builder?
I have tried union but it’s not working.
I hope this example gives idea , Try this one
use multiple if statement and get data into DB using joins .
function datatables($request) {
$data = $this->leftJoin('blog_category', 'blog_category.blog_category_uuid', '=', 'blog_detail.blog_category_uuid')
->where('blog_detail.blog_detail_is_deleted', 'NO');
if ($request->search['value'] != null && $request->search['value'] != '') {
$keyword = $request->search['value'];
$data = $data->where(function($query) use ($keyword) {
// $query->orWhere('activity_type.activity_type_name', 'LIKE', '%' . $keyword . '%');
$query->orWhere('blog_detail.blog_detail_title', 'LIKE', '%' . $keyword . '%');
});
}
if (isset($request->order[0]['dir'])) {
$data = $data->orderBy('blog_detail.blog_detail_id', $request->order[0]['dir']);
} else {
$data = $data->orderBy('blog_detail.blog_detail_created_date');
}
$datacount = $data->count();
$dataArray = $data->select('blog_detail.*', 'blog_category.blog_category_name' , DB::raw('DATE_FORMAT(blog_detail.blog_detail_created_date,"%Y-%m-%d") as blog_detail_date'));
if ($request->length == -1) {
$dataArray = $dataArray->get();
} else {
$dataArray = $dataArray->skip($request->start)->take($request->length)->get();
}
return [$datacount, $dataArray];
}
In laravel you can create a model for product say Product. Then the query will be like
$products = Product::where('product_status', '1');
if ($request->input('minimum_price') && $request->input('maximum_prize')) {
$products = $products->whereBetween('product_prize', array($request->input('minimum_price'), $request->input('maximum_prize')));
}
if ($request->input('brand')){
$brand_filter = implode("','", $request->input('brand'));
$products = $products->whereIn('product_brand', $brand_filter);
}
$products = $products->get();
after the execution $products contains the products after query.

looking for similar data with like

In my controller, I send a string to my model. when I send that, the string is encoded and it makes problem in my query because I use LIKE Operator.
controller :
$this->base_model->get_post('football');
model:
function get_post($string){
$this->db->select('*');
$this->db->like('title' , $string , 'both');
$query = $this->db->get('post');
return $query->result();
}
In the model when I echo the $string it becomes to %D9%81%D9%88%D8%AA%D8%A8%D8%A7%D9%84 not football so the query can't fetch any data from my database
Decode it.
function get_post($string){
$decrypt = $this->encryption->decode($string);
$this->db->select('*');
$this->db->like('title' , $decrypt , 'both');
$query = $this->db->get('post');
return $query->result();
}

how to check out of function in codeigniter

i am beginner in codeigniter and want to check the out put of a function in codeigniter so that i can debug it but i am unable to do so for example here is my function in my managementmodel and i have put the slashes before the print_r function. when i want to see the result i remove these but i do not know where to see the result if in view then how please help me. thanks
function department()
{
$sql = "SELECT * FROM department ORDER BY id
ASC";
$query = $this->db->query($sql);
// echo "<pre>";
// print_r($query->result_array());exit;
return $query->result();
}
function get_test_list_info()
{
$sql = "SELECT * FROM test_list ORDER BY id
DESC";
$query = $this->db->query($sql);
//print_r($query->result_array());exit;
return $query->result();
}

Codeigniter active query error

Here is my query
$this->db->select('amount AS SavingAmount');
$query = $this->db->get_where('cashman_trial_balance',array('category_id'=>'176','clientId'=>$user['UserID'],'year'=>$year));
On checking in browser its generating this query below
SELECT *, *, `amount` AS SavingAmount FROM (`cashman_trial_balance`) WHERE `category_id` = '176' AND `clientId` = '122' AND `year` = '2015/2016'
I dont know from where the two stars coming from?
Controller code
$som_var = $this->client->statistics();
Model code
function statistics()
{
/* Some other queries */
$b = $this->get_balances();
}
function get_balances()
{
$this->db->select('amount AS SavingAmount');
$query = $this->db->get_where('cashman_trial_balance',array('category_id'=>'176','clientI‌​d'=>$user['UserID'],'year'=>$year));
return $query->result();
}
use this
$query = $this->db->query("SELECT amount AS SavingAmount FROM table_name WHERE category_id = '176'
AND clientId = '$user['UserID']' AND 'year'= '$year') ");
$result = $query->result_array();
return $result;
You can use the simple way
$query = $this->db->query("SELECT amount AS SavingAmount FROM cashman_trial_balance WHERE category_id = '176' AND clientId=$user['UserID'] AND year=$year");

Resources