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.
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'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();
}
My model
public function getLogs()
{
$this->db->limit(5);
$query = $this->db->get('tbllogs');
return $query->result();
}
Try this
$this->db->order_by("id", "desc");
$this->db->limit(5);
$query = $this->db->get('tbllogs');
return $query->result();
Or for old and latest logic make two links in your view with orderBy query parameter in it.
Old
Latest
In your controller method
if ($this->input->get('orderBy') == 'old') {
$this->db->order_by("id", "asc");
} else {
$this->db->order_by("id", "desc");
}
$this->db->limit(5);
$query = $this->db->get('tbllogs');
return $query->result();
With where condition
$result = $this->db->order_by('id', 'DESC')->limit(5)->get_where($tablename,$where);
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
}
i am trying to execute query like: select*from bookdetails where quantity>0 ORDER BY created_date DESC
but i am getting error like Call to a member function num_rows() on a non-object in...
Please help me to solve this issue.
public function get_all_book_list_ByCreatedDateDSC($limit, $start,$sortsesval)
{
$this->load->database();
$this->db->limit($limit, $start);
$query=$this->db->join('coverdetails', 'coverdetails.cover_id = bookdetails.cover_id')->order_by('created_date', 'DESC')->get_where('bookdetails',array('quantity >', '0'));
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$data[] = $row;
}
return $data;
}
return false;
}
Try This:
$this->db->select('*');
$this->db->where('quantity >', $id);
$this->db->join('coverdetails', 'coverdetails.cover_id = bookdetails.cover_id');
$this->db->order_by('bookdetails.created_date', 'desc');
$this->db->limit($limit, $start);
$query = $this->db->get('bookdetails');
if($query -> num_rows() == 1)
return $query->result_array();
else
return false;
Your error message is telling you that $query is not an object. You cant call a member function on a non-object!
Your "join" method is not returning an object.
Try putting print_r($query) or var_dump($query) after you set $query, to find out exactly WHAT it is.
Try your function like this
public function get_all_book_list_ByCreatedDateDSC($limit, $start,$sortsesval)
{
$this->load->database();
return $this->db
->join('coverdetails', 'coverdetails.cover_id = bookdetails.cover_id')
->order_by('created_date', 'DESC')
->limit($limit, $start)
->get_where('bookdetails',array('quantity >', '0'))
->result_array();
}
When your query returns null(found no record) you wont be able to access the object because in $query it is null. You need to return the data in array format so use result_array. For detail see here; Using this you dont need to loop through object. It will give you ready-made array.
In the controller you can check if the result is null or array.