where to put limit and offset? - php

this code is written by Codeigniter and it is in the models folder
I need to add
$this->db->limit($limit,$offset);
to following code. Where do I put it? The model code is here
$this->db->where('comments.product_id', $product_id);
$this->db->select('comments.comment,comments.ratings,comments.comment_users_id,comment_users.name');
$this->db->from('comments');
$this->db->join('comment_users', 'comment_users.id = comments.comment_users_id');
$this->db->join('product', 'product.product_id=comments.product_id');
$this->db->order_by('comments.id', 'DESC');
return $this->db->get()->result();

Order is doesn't matter just do like this
$this->db->where('comments.product_id', $product_id);
$this->db->select('comments.comment,comments.ratings,comments.comment_users_id,comment_users.name');
$this->db->from('comments');
$this->db->join('comment_users', 'comment_users.id = comments.comment_users_id');
$this->db->join('product', 'product.product_id=comments.product_id');
$this->db->order_by('comments.id', 'DESC');
$this->db->limit($limit,$offset); //here added
return $this->db->get()->result();

Related

LIKE does not work in codeigniter

I am using LIKE to see if the similar order_id exists or not in my db. When i run the application, i see that this below code doesnt return any value. It is returning only null value.
$temp_orders = $this->transaction_model->get_similar_user_temp_transaction($cart_order_id);
function get_similar_user_temp_transaction($order_id)
{
$query = $this->db->select('*')
->from('temp_transaction')
->like('order_id', $order_id)
->get();
return ($query->num_rows() < 1) ? null : $query->result();
}
Kindly check the above code. Help would be appreciated
try your code after changing statement order
function get_similar_user_temp_transaction($order_id)
{
$query = $this->db->select('*')
->like('order_id', $order_id)
->from('temp_transaction')
->get();
return ($query->num_rows() < 1) ? null : $query->result();
}
EDIT
If you want to match exact order_id, use where instead of like
function get_similar_user_temp_transaction($order_id)
{
$query = $this->db->select('*')
->where('order_id', $order_id)
->from('temp_transaction')
->get();
return ($query->num_rows() < 1) ? null : $query->result();
}
write like in where cluase
$query = $this->db->select('*')
->from('temp_transaction')
->where("order_id LIKE '%$order_id%'")->get();
Here SQL like not work, because like is not working with INT values
You have two options :-
1. ->like(CONVERT(order_id, CHAR(16), $order_id)
2. Use ->where(order_id, $order_id)
If you are trying to match exact use where clause.
//dont need ->select('*') //excluding select gives same equivalent
//dont need from
return $this->db->where('order_id', $order_id)->get('temp_transaction')->result();

count number of rows with detail through query in codeigniter

I am making a travel agency website in codeigniter. I am getting destination of africa from my database and displaying on a page.
here is my code of controller:
public function africa() {
$data['africa']= $this->Travel->africa_des();
$this->load->view('africa', $data);
}
my Model:
function africa_des() {
$this->db->distinct();
$this->db->select();
$this->db->from('travels_detail');
$this->db->like('region', 'africa');
$this->db->limit(5);
$query= $this->db->get();
return $query->result_array();
}
I want to get total number of flights of each destination that i have in my database. For instance I have 25 flights of Abuja so I want to get that number with Abuja destination.
Anyone have any idea?
Try this one
public function africa_desc(){
$this->db->distinct();
$this->db->select('*');
$this->db->from('travels_detail');
$this->db->where('region', 'africa');
$this->db->limit(5);
$query= $this->db->get();
return $query->result_array();
}
When you are using $this->db->distinct() you must use $this->db->group_by('column_name') see this answer https://stackoverflow.com/a/19509869/6098616. In your case I will assume that destination is your column name.
public function africa_desc(){
$this->db->select('*');
$this->db->distinct();
$this->db->group_by('destination');
$this->db->from('travels_detail');
$this->db->where('region', 'africa');
$this->db->limit(5);
$query= $this->db->get();
return $query->result_array();
}
Hope this will help.
Try this to count flights
public function africa_desc(){
$this->db->select('*');
$this->db->distinct();
$this->db->group_by('destination');
$this->db->from('travels_detail');
$this->db->where('region', 'africa');
$this->db->limit(5);
$query= $this->db->get();
return $query->num_rows();
}

Codeigniter $this->db->where not working

I have the following codes in my model.
This is my original code.
I have tested that the value of my $category_id = 1
function getSubCategory($category_id){
$this->db->select('id, subcategory');
$this->db->from('subcategory');
$this->db->where('category_id', $category_id);
$this->db->order_by("subcategory", "asc");
$query = $this->db->get();
return $query->result();
}
however I am encountering a problem where $this-db->where is not working.
function getSubCategory($category_id){
$cat = 1;
$this->db->select('id, subcategory');
$this->db->from('subcategory');
$this->db->where('category_id', $cat);
$this->db->order_by("subcategory", "asc");
$query = $this->db->get();
return $query->result();
}
I tried this line of code just to make sure that I am not doing any syntax error. And with these line of codes I am getting what I wanted. I wonder what makes my original code not work.
It would be a great help if someone can help. Thanks.
It turns out that the problem I was having is from the AJAX function that I was calling to the call the method in my model.
The answer is seen here
AJAX not displaying result

Multiple Where in Condition in Codeigniter

I am using codeigniter active record query.
function select_in($table,$cond)
{
$this->db->select('*');
$this->db->from($table);
$this->db->where_in('brand_id',$cond);
$query = $this->db->get();
//echo $this->db->last_query(); exit;
return $query;
}
I need to pass another one where_in condition in this query.Is it Possible?
Try with -
$this->db->where_in('brand_id',$cond);
$this->db->where_in('field' , $cond_new);

CodeIgniter COUNT with active record?

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.

Categories