count number of rows with detail through query in codeigniter - php

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();
}

Related

Getting data between two date range in codeigniter

I'm trying to fetch data between dates. The date select from date-picker. the formate is like this 04/13/2020 - 04/13/2020 select from date-picker. my query is like this
public function reservation($reservation)
{
$this->db->select("*");
$this->db->from('details');
$this->db->where("DATE_FORMAT(date,'%Y-%m-%d') > '$reservation'");
$query = $this->db->get();
return $query->result();
}
I hope This will work for you
public function reservation($first_date,$second_date)
{
$this->db->select("*");
$this->db->from('details');
$this->db->where("DATE_FORMAT(date,'%Y-%m-%d') >='$first_date'");
$this->db->where("DATE_FORMAT(date,'%Y-%m-%d') <='$second_date'");
$query = $this->db->get();
return $query->result();
}
try this:
$this->db->where("DATE_FORMAT(start_date,'%Y-%m-%d')",'>=',$first_date)
->where("DATE_FORMAT(end_date,'%Y-%m-%d')",'<=',$first_date);

Codeigniter: How to get data from id using two table by passing an id

I have two join tables; parent and student. I have to update both tables from one button click. For that, I want to write a function "get data by id". I managed to write that code only for one table.
How do you write the following code if I want to get data from two tables? if p_id (parent id) is the foreign key?
Model
function get_by_id($id)
{
$this->db->from('student');
$this->db->where('p_id',$id);
$query = $this->db->get();
return $query->row();
}
Controller
public function ajax_edit($id)
{
$data = $this->Model_Action->get_by_id($id);
echo json_encode($data);
}
Hi I think you are looking for this. I use a sample from your code:
function get_by_id($id)
{
$this->db->from('student');
$this->db->join('table_b', 'student.p_id=table_b.p_id');
$this->db->where('student.p_id',$id);
$query = $this->db->get();
return $query->row();
}
Actually you can find more here
function get_by_id($id)
{
$this->db->select('*')
$this->db->from('student');
$this->db->join('parent','student.p_id=parent.p_id');
$this->db->where('student.p_id',$id);
$query = $this->db->get();
return $query->row();
}

where to put limit and offset?

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();

Number of decimal with select_avg() in CodeIgniter

Is there any way to show only one decimal using select_avg() in CodeIgniter?
$this->db
->select_avg('Stars')
->from('Reviews')
$query=$this->db->get();
return $query->result();
Try like this
$this->db->select_avg(ROUND('Nr_stele'))->from('Recenzii as r');
$query = $this->db->get();
return $query->result_array();
Check SQL ROUND() Function and $this->db->select_avg() in codeigniter

How to select MONTH() and YEAR() in codeigniter active record

I am working in codeigniter, my problem is i want to select month & year in below query.. it returns month but i need month & year as a single value.. please help..
public function get_content(){
$this->db->select('MONTH(newsroom_publish_date) as month');
$this->db->from('content_table');
$this->db->where('status', "Active");
$query = $this->db->get();
return $query->result();
}
Try this:
public function get_content(){
$this->db->select('CONCAT(MONTH(newsroom_publish_date)),'-',(YEAR(newsroom_publish_date)) as year_month);
$this->db->from('content_table');
$this->db->where('status', "Active");
$query = $this->db->get();
return $query->result();
}
Same as MONTH function in MySQL, use YEAR function for the same.
Do like this:
public function get_content(){
$this->db->select('MONTH(newsroom_publish_date) as month, YEAR(newsroom_publish_date) as year');
$this->db->from('content_table');
$this->db->where('status', "Active");
$query = $this->db->get();
return $query->result();
}
YEAR returns from given timestamp.
Let me know for more help!

Categories