This is a search function I made, it gets the search term and displays fine. HM_Jobs has 3 options for JobStatus: Open, Accepted or Complete.
However the search is also pulling results from the Accepted and Complete rows when I run it, why isn't the WHERE statement stopping this from happening?
function search_jobs($search) //This function checks a search term against the job title and description.
{
$this->db->select('*');
$this->db->from('HM_Jobs');
$this->db->like('JobTitle',$search);
$this->db->or_like('JobDescription',$search);
$this->db->where('JobStatus','Open');
$query = $this->db->get();
return $query->result();
}
Try this
$this->db->select('*');
$this->db->from('HM_Jobs');
$this->db->where("(JobTitle LIKE '$search' OR JobDescription LIKE '$search')" );
$this->db->where('JobStatus','Open');
$query = $this->db->get();
You can see your query executed by using echo $this->db->last_query()
Your query creates like
where JobTitle like 'search' or JobDescription like 'search' and JobStatus='Open'
But the query you need require something like
where (JobTitle like 'search' or JobDescription like 'search') and JobStatus='Open'
Try using the following query
function search_jobs($search)
{
$query = $this->db->select('*')
->from('HM_Jobs')
->where('JobStatus','Open')
->where("(`JobTitle` LIKE '%$search%' OR
`JobDescription` LIKE '%$search%')",null,false)
->get();
return $query->result();
}
Related
I want to get data from two tables, the second table is for rating so i want to get rating of products simultaneosly. Below code is not working for me if i change
$this->db->select('dg_products.',', AVG(dg_rating.rating) As
averageRating');
to
$this->db->select('*');
then it is working.
Please help to sort out my issue.
public function get_rating()
{
$this->db->select('dg_products.*','*, AVG(`dg_rating.rating`) As averageRating');
$this->db->from('dg_products');
$this->db->join('dg_rating', 'dg_products.id = dg_rating.product_id','left');
$this->db->where('dg_products.is_featured_prod','1');
$this->db->group_by("dg_products.id");
$query = $this->db->get();
$result = $query->result();
return $result;
}
Try it like this :
$this->db->select('dg_products.*, AVG(`dg_rating.rating`) As averageRating');
you just have an unneeded quotes in there.
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.
I wrote this code for search in my table search for a string in posts where show = 1.
but when i run this code where('show',1) not affected and rows where having show = 0 returned from database, what this problem?
public function json_search($str)
{
$out['Threadt'] = $this->db
->select('id')
->from('posts')
->like('title',$str)
->or_like('story',$str)
->or_like('description',$str)
->or_like('full-story',$str)
->where('date <',time())
->where('show',1)
->get()
->result_array();
}
you should try this, this should work:
function json_search($str)
{
$where = "( story like '%".$str."%' OR title like '%".$str."%' OR description like '%".$str."%' OR full-story like '%".$str."%' )";
$out['Threadt'] = $this->db
->select('id')
->from('posts')
/*->like('title',$str)
->or_like('story',$str)
->or_like('description',$str)
->or_like('full-story',$str)*/
->where( $where, false, false )
->where('date <',time())
->where('show',1)
->get()
->result_array();
}
See the commented part above, when you're using it like this, you are appending OR clauses and this interferes with the show clause.
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;
I'm trying to create pagination in codeigniter and I have it working, but I have a small issue. It seems to be loading all the entries in my database and not the selected ones I want.
public function original_count() {
$this->db->where('type', 'Original');
return $this->db->count_all("story_tbl");
}
I know that whats happening is that the last line is overrighting my previous statements. I can't seem to find a way around it though. I tried just a staight sql statement and then returning it, but I could not get that to work either.
this was my statement...
SELECT COUNT(*) FROM story_tbl where type = 'Original';
Some help would much appreciated! :)
CI has inbuilt count method
count_all_results()
Permits you to determine the number of rows in a particular Active Record query. Queries will accept Active Record restrictors such as where(), or_where(), like(), or_like(), etc. Example:
https://www.codeigniter.com/userguide2/database/active_record.html
$total_count = $this->db->count_all_results('story_tbl', array('type' =>'Original'));
You could also use the built-in num_rows() function...
$query = $this->db->where('type', 'original')->get('story_tbl');
return $query->num_rows();
First Try this one.
$query = $this->db->where('tbl_field', 'value')
->get('your_table');
return $query->num_rows();
Besides this Codeigniter has it own function like the following.
$this->db->where('tbl_field', 'value')
->get('your_table');
return $this->db->count_all_results();
Or use this.
return $this->db->count_all('your_table');
Where wont work on count_all condition.. you can use below method to find out total number of rows..
public function count_all() {
$this->db->select ( 'COUNT(*) AS `numrows`' );
$this->db->where ( array (
'type' => 'Original'
) );
$query = $this->db->get ( 'story_tbl' );
return $query->row ()->numrows;
}