I've tried to make a search field with two parameters. One for name and the other one for location. But my code returns only the results for the first parameter. Maybe someone could figure out what's wrong.
Below it's my code.
Mode:
public function search_job($keyword, $location){
$query = $this->db->like('name', $keyword)
->like('location', $location)
->or_like('description', $keyword)
->get('jobs');
return $query->result_array();
}
Controller:
public function search_job(){
$location = $this->input->post('location');
$keyword = $this->input->post('keyword');
if ($this->input->post('submit')) {
$this->Job_model->search_job($keyword, $location);
}
}
I think your query is getting messed up when no $keyword is passed, try below query.
Code :
public function search_job($keyword, $location){
$this->db->select('*');
$this->db->from('jobs');
if(!empty($keyword)) {
$this->db->group_start();
$this->db->like('name', $keyword);
$this->db->or_like('description', $keyword);
$this->db->group_end();
}
$this->db->like('location', $location);
$query = $this->db->get();
return $query->result_array();
}
Related
public function cmplFirstRdDetails($userMobileNo) {
$this->db->from('records');
$this->db->like('player1', '{"PhoneNumber":"$userMobileNo"}');
$query = $this->db->get();
return $query->result();
}
This code is not working for me.
I have made a query in Laravel Eloquent to search in table.
public function searchContest($search){
$category = [];
$area = [];
if(isset($search['area'])){
$area = $search['area'];
}
if(isset($search['category'])){
$category = $search['category'];
}
$qry = self::whereIn('area',$area)
->whereIn('category', $category)
->get();
var_dump($query);
return;
}
But sometimes, area or category is empty and whereIn does not work with it. I'm not able to find any working solutions in the net. How can I make such a query?
Or you can take advantage of the conditional clauses as here
DB::table('table_name')
->when(!empty($category), function ($query) use ($category) {
return $query->whereIn('category', $category);
})
->when(!empty($area), function ($query) use ($area) {
return $query->whereIn('area', $area);
})
->get();
$q = self::query();
if (isset($search['area'])) {
$q->whereIn('area', $search['area']);
}
if (isset($search['category'])) {
$q->whereIn('category', $search['category']);
}
$qry = $q->get();
You can use query scope inside the entity
public function scopeArea($query, $ids)
{
if (! $ids) {
return ;
}
return $query->whereIn('area', $ids);
}
public function scopeCategory($query, $ids)
{
if (! $ids) {
return ;
}
return $query->whereIn('category', $ids);
}
Now you can build the query
$entity
->area($area)
->category($category)
->get();
https://laravel.com/docs/5.7/eloquent#query-scopes
I have this function in model
function select($table,$match,$or){
$this->db->select('*');
$this->db->from($table);
$this->db->where($match)->or_where($or);
$query = $this->db->get();
$query_result = $query->result();
return $query_result;
}
and in controller
$post = array("class"=>"XI","section"=>"A","stream"=>"Medical");
$data = $this->main_model->select("class_subjects",array("class"=>$post['class'],"section"=>"all","stream"=>$post['stream']),$post);
print_r($data);
I am not getting any error but this is printing whole data of table. how can i match class='XI' and stream='Medical' and (section='A' or section='all')
Try this
function select($table,$match,$or)
{
$query = $this->db
->select("*")
->from($table)
->group_start()
->where($match)
->group_end()
->group_start()
->or_where($or)
->group_end()
->get();
$strSql = $this->db->last_query();
return $query->result();
}
if that won't work - print out the $strSql Variable and post the Query here.
I'm having a little trouble in retrieving data in multiple tables using codeigniter.
This is the code i'm using to retrieve data in my model which is working well.
function retrieve_experience($alumni_id)
{
$this->db->select('*');
$this->db->from('experience');
$this->db->where('alumni_id',$alumni_id);
$query = $this->db->get();
return $query;
}
function retrieve_education($alumni_id)
{
$this->db->select('*');
$this->db->from('education');
$this->db->where('alumni_id',$alumni_id);
$query = $this->db->get();
return $query;
}
Now i tried using a simplified code but fails to display the data. here is the code in my model
function retrieve_all_data($alumni_id)
{
$this->db->select('*');
$this->db->from('experience');
$this->db->join('education','education.alumni_id=experience.alumni_id');
$this->db->where('experience.alumni_id',$alumni_id);
$query=$this->db->get();
return $query->result_array();
}
In my controller, i used this code to retrieving data in my model
function display()
{
$alumni_id = $this->session->userdata('alumni_id');
$data['all_data'] = $this->Alumni_model->retrieve_all_data($alumni_id);
$data['main_content'] = 'alumni_home';
$this->load->view('includes/template', $data);
}
and for the display i used this code
foreach($all_data as $results)
{
/** data from experience table **/
$results['company_name'];
$results['company_address'];
/** data from education table **/
$results['school_name'];
$results['field_of_study'];
}
I cant display anything at all. Please help
Try the below code,
I believe you'll want something like this:
function retrieve_all_data($alumni_id)
{
$this->db->select("e.*,edu.*");
$this->db->from("experience e");
$this->db->join("education edu", "edu.alumni_id = e.alumni_id",'left');
$this->db->where('e.alumni_id',$alumni_id);
$this->db->group_by('e.exp_id');
$query = $this->db->get();
return $query->result_array();
}
Hope below mentioned function should return data that you expected.
function retrieve_all_data($alumni_id)
{
$this->db->select('*');
$this->db->from('experience ex');
$this->db->join('education ed','ed.alumni_id=ex.alumni_id');
$this->db->where('ex.alumni_id',$alumni_id);
$query=$this->db->get();
return $query->result_array();
}
I have function to fetch the products from ci_products table
the code is as follows
function get_product_selections($product_ids = array())
{
$this->db->select('*');
$this->db->from('ci_products');
$this->db->where_in('product_id', $product_ids);
$products = $this->db->get();
return $products;
}
now I want to fetch from where user_id is current user_id
I have tried few things but not working
what I have tried`
function get_product_selections($product_ids = array())
{
$this->db->select('*');
$this->db->from('ci_products');
$this->db->where_in('product_id', $product_ids);
$this->db->where('user_id',$this->session->userdata('user_id'));
$products = $this->db->get();
return $products;
}
$data = $this->db->get_where('product_ids',$product_ids);
$this->db->where_in('product_ids',$data);
$this->db->where('user_id',$this->session->userdata('user_id'));
$query = $this->db->get();
return $query->result_array();
Try this one :
In your first code:
function get_product_selections($product_ids)
{
$this->db->select('*');
$this->db->from('ci_products');
$this->db->where('product_id', $product_ids);
$products = $this->db->get();
return $products->result_array();
}
what do you mean when you put array() in the parameter? you want to retrieve an array() of products?
function get_product_selections()
{
$this->db->select('*');
$this->db->from('ci_products');
$this->db->where('user_id',$this->session->userdata('user_id')); // the user who is currently login
$products = $this->db->get();
return $products->result_array(); // return the results
}