Where to add the like clause in php? - php

where can I put the like statement on this code? I would want to allow the user to search any documents that are similar to the entered text it would be document like '%$search%;
public function getDoc($param=''){
if($this->User->PrincipalType!="All")$where['tbl_document.PrincipalType']=$this->User->PrincipalType;
if(!isset($param['RecordID']) || $param['RecordID']==''){
$where['tbl_document.Status !=']='Removed';
$this->db->select('tbl_document.*,cr.FirstName,cr.MiddleName,cr.LastName,up.FirstName as UFName,up.MiddleName as UMName,up.LastName as ULName');
$this->db->from('tbl_document');
$this->db->join('tbl_user `cr`', 'tbl_document.CreatedBy = cr.ID','left');
$this->db->join('tbl_user `up`', 'tbl_document.UpdatedBy = up.ID','left');
if(isset($param['DocTrack']) && $param['DocTrack']!='') $where['tbl_document.ID']=$param['DocTrack'];
$this->db->where($where);
if(isset($param['DocTrack']) && $param['DocTrack']!='')$this->db->or_where('tbl_document.Title',$param['DocTrack']);
$this->db->order_by('DateCreated', 'desc');
$query = $this->db->get();
$return=$query->result();
}

Assuming you want to search for $param['DocTrack'] then you can use below two approaches for like query execution as below:
1. $this->db->like('tbl_document.Title','%'.$param['DocTrack'].'%')
2. $this->db->where("tbl_document.Title LIKE '%".$param['DocTrack']."%'");
Hope it helps you :)

Related

where clause not working with like query

I have a search query and when I search all like keywords work properly the records are showing with the matched criteria, but the problem is that where clause in not working.
$position = $this->session->set_userdata('position', $this->input->post('position'));
$adress_all = explode(",", $this->session->userdata('address'));
$this->db->like('address', $adress_all[0]);
$this->db->or_like('game', $this->session->userdata('skills'));
$this->db->or_like('gender', $this->session->userdata('coach'));
$this->db->or_like('Positions', $this->session->userdata('position'));
$this->db->where('type', 'private');//all other type coaches is also showing up
$sql = $this->db->get('coach');
You need to use Grouping LIKE of codeigniter
$this->db->group_start();
$this->db->like('address', $adress_all[0]);
$this->db->or_like('game', $this->session->userdata('skills'));
$this->db->or_like('gender', $this->session->userdata('coach'));
$this->db->or_like('Positions', $this->session->userdata('position'));
$this->db->group_end();
$this->db->where('type', 'private');
$sql = $this->db->get('coach');
Hope this works(need to test on your DB)
See Doc
Try below things.
$this->db->where('type', 'private');//all other type coaches is also showing up
$this->db->where("(address LIKE '%". $adress_all[0]."%' OR game LIKE '%".$this->session->userdata('skills')."%' OR gender LIKE '%".$this->session->userdata('coach')."%' OR positions LIKE '%".$this->session->userdata('position')."%')",null,false);
$sql = $this->db->get('coach');

Codeigniter : how to write a query when the inside of the table have value ""

I've got a table in which a field contains pattern Like this [{"vendor":"10","status":"paid"}] :
table
I want to make a query 'like' in codeigniter , but I got an error:
model :
function get_total_order($id_vendor){
$this->db->like('payment_status', 'vendor":"'.$id_vendor.'","status":"due');
$this->db->from('sale');
return $this->db->count_all_results();
}
view :
<?php
$new_order = $this->crud_model->get_total_order($this->session->userdata('vendor_id'));
echo "<h1>".$new_order."</h1>";
?>
when i run this, i got blank page, how i fix this?
thanks.
Since you use "Like" query type, you should add '%' in the query argument or send a complete argument:
function get_total_order($id_vendor)
{
$this->db->like('payment_status', '%vendor":"'.$id_vendor.'","status":"due%');
$this->db->from('sale');
return $this->db->count_all_results();
}
Try this:
function get_total_order($id_vendor){
$this->db->like('vendor',$id_vendor);
$this->db->like('status',"due");
$this->db->from('sale');
return $this->db->count_all_results();
}
if your searching json data so you have pass the data in like query and like query data should be look like data inside the table how it looks .
your query should be something like this
<?php
$id_vendor =123;
$ss= '%"vendor":"'.$id_vendor.'","status":"due"%';
$sssss ="select * from sale where payment_status like '$ss' ";
echo $sssss;
query look like this
select * from sale where payment_status like '%"vendor":"123","status":"due"%'
?>
and also you can use wildcard (%) more place with your wish.
You can customize where as per your requirement with and condition or another condition.
$where = "payment_status like '%$id_vendor%' OR status like '%$status%'";
$this->db->where($where);
try this one:
Because 'like is time consuming.
function get_total_order($id,$vendor)
{
$this->db->where('vender', $id);
$this->db->where('status',$vendor);
$this->db->get('sale');
$result=$res->result_array();
return $result;
}
You can use $this->db->where_in() like below:-
$names = array('Frank', 'Todd', 'James');
$this->db->where_in('username', $names);
// Produces: WHERE username IN ('Frank', 'Todd', 'James')
For more details, please check below link:-
https://www.codeigniter.com/userguide2/database/active_record.html

CodeIgniter Select Statement with Where clause

Hi I'm new to CodeIgniter and I just want to know How will I query from my MySql Db, a Select Statement with a where clause, I know it can be searched from the net but whenever I try something I get errors, It's really frustrating. The string in the Where clause will be coming from a User Input. Thanks guys!
You can do as Mehedi-PSTU stated, however it seems as though you're a little new to this, so here's some extra information:
I'll copy Mehedi-PSTU for the most part here.
$this->get->where('column_name', $equals_this_variable);
$query = $this->db->get('table_name');
This will store the query object in the variable $query.
if you wanted to convert that to a usable array, you just perform to following.
$results = $query->result_array();
Or you can loop through it like this:
foreach($query->result_array() as $result){
// Perform some task here.
}
A better or even full understanding can probably come from:
http://ellislab.com/codeigniter/user-guide/database/active_record.html
Try something like this
$this->db->where('db_attr', $var);
return $this->db->get('table');
Try this one.
$id = 'your id';
$this->db->select("*");
$this->db->from("table_name");
$this->db->where('id','$id');
$query = $this->db->get();
return $query->result_array();
In Codeigniter with Method Chaining Style :-
$data['getData'] = $this->db->get_where('table_name',array('column_name'=>$var))->result_array();

Perform sql operations for this special case

Sir,
I am facing a problem and it is shown below:
I want to perform where opertion on the output of $this->db->select('*')->from('table'); only when cond1 satisfies
$result = $this->db->select('*')->from('table');
if(cond1)
{
I want to perform $result->where(); operation
}
Is it possible and i so what is the exact syntax to do it
try something like this:
$this->db->select('*');
$this->db->from('table');
if ($cond1)
$this->db->where(...)
$query = $this->db->get();
$con1 is should be a variable and it should has a value or Null. $con1 like a Boolean.1 or 0 .if 1(true) happening it'll execute the if block if not it'll execute the else block.
Looks like you want to run two different queries. Assuming that $cond1 is not dependent upon $result (in which case all bets are off), you can do something like
if(cond1)
$result = $this->db->select('*')->from('table')->where(...);
else
$result = $this->db->select('*')->from('table');

codeigniter search

hello i have little problem
i want search in mysql by name
i have this code in search model
$this->db->limit($limit, $start);
$this->db->like("name",$by);
$res = $this->db->get('walls');
if ($res->num_rows() > 0)
return $res->result();
result for $by = "megan"; is working
but when $by = "megan fox"; is not working
in mysql name i have "megan fox";
I tried urldecode but to no avail.
i using codeigniter framework..
any ideas?
$this->db->limit($limit, $start);
$this->db->like("name",$by);
$res = $this->db->get('walls');
if ($res->num_rows() > 0)
return $res->result();
You might want to add BOTH,LEFT,RIGHT on your like() clause example
$this->db->like("name",$by,'BOTH'); // left and right wild card %name%
$this->db->like("name",$by,'LEFT'); // left wild card %name
$this->db->like("name",$by,'RIGHT'); // right wild card name%
You can read more at http://ellislab.com/codeigniter/user-guide/database/active_record.html
You can first split the $by variable into separate values with something like this:
$by = explode(" ", $by);
and then try to make the like clause like this
$this->db->like("name",$by[0]);
$this->db->or_like("name",$by[1]);
It will produce something like this
WHERE name LIKE '%megan%' OR name LIKE '%fox%'
This assumes that you always pass 2 variables to the $by, seperated by space.
You have to adjust it to make sure it works in every case, for example when there is only one variable passed to the $by you should do a check for it.
And please note that it will be fairly slower than splitting the name and the surname into two separate fields in the table and querying each of them for a specific name or surname. You should do this if you care for optimization.
Use urldecode function to solve this
Try this into your model
$by= urldecode($by);
$this->db->limit($limit, $start);
$this->db->like("name",$by);
$res = $this->db->get('walls');
if ($res->num_rows() > 0)
return $res->result();

Categories