Number of decimal with select_avg() in CodeIgniter - php

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

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

How to make a join and search in data using codeigniter and Php

I make a join between two tables using codeigniter framework and this is my query:
public function SearchDataUnderCondition($firsttable,$secondtable,$data)
{
$this->db->select("atm.* , tender.status as tenderstatus");
$this->db->from($firsttable);
$this->db->join($secondtable,'atm.id_tender=tender.id');
$this->db->where('tenderstatus','1');
$this->db->like('serial', $data);
$sql = $this->db->get();
return $sql->result();
}
I am using database model when is remove
$this->db->where('tenderstatus','1');
from my code operation done and i get result but I want to make search under this condition. what is my problem?
Try this
public function SearchDataUnderCondition($data)
{
$this->db->select('atm.* , tender.status');
$this->db->from('atm');
$this->db->join('tender','atm.id_tender=tender.id');
$this->db->where('tender.status','1');
$this->db->like('serial', $data);
$sql = $this->db->get();
return $sql->result();
}
Try this
$this->db->where('tender.status','1');
I think, we can not aliases in where clause
Using aliases (and also making sure you want and WHERE AND LIKE clause):
$fist_table = 'atm';
$second_table = 'tender';
$this->db->select('aliasone.* , aliastwo.status as "tenderstatus"');
$this->db->from("$first_table as aliasone");
$this->db->join("$second_table as aliastwo", "aliastwo.id = aliasone.tender_id");
$this->db->where('aliastwo.status','1');
$this->db->like('serial', $data);
return $this->db->get();

Codeigniter get_where query

public function countTasks(){
$q = $this->db->get_where('tasks', array('task_status' => 1));
return $this->db->count_all_results();
}
This is the function I have in my model that counts all rows in my 'tasks' table where the column 'task_status' is equal to 1. It basically returns an integer that I use for my pagination. The query is not working as it doesn't return any data even though I know I have 4 rows that match that requirement in my table.
I was gonna use the returned value for my pagination. Please help. Thanks
Try
return $q->num_rows();
Or
$this->db->where('task_status', '1');
$query = $this->db->get('tasks');
return $query->num_rows();
$this->db->count_all_results(); is not working after the get function, so do this instead:
public function countTasks(){
$q = $this->db->get_where('tasks', array('task_status' => 1));
return return $q->num_rows();
}

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

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

Categories