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);
Related
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();
}
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();
}
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
I've got a sorting problem. I get data properly from the database, but the records are not sorted as I want: current date records displayed first and then other records.
I've tried to use order_by but later date records are displayed first. I want to get current date records first. How to achieve it...??
See my model.
public function getshopsponsered()
{
$this->db->select('*');
$this->db->from('shop_sponsered');
$this->db->order_by('startdate', 'DESC');
$query = $this->db->get();
$shop_list = array();
foreach($query->result() as $row)
{
$sm = new Shop_sponsered_model();
$sm->setId($row->id);
$sm->setStartdate($row->startdate);
$sm->setEnddate($row->enddate);
$sm->setDisplayIndex($row->display_index);
//get business name//
$this->db->select('name');
$this->db->from('business');
$this->db->where('id',$row->businessid);
$query = $this->db->get()->row();
if($query > 0)
{
$sm->setBusinessid($query->name);
}
array_push($shop_list,$sm);
}
return $shop_list;
}
change the $query instead of.
$query_outter = $this->db->get();
$shop_list = array();
foreach($query_outter->result() as $row)
{...}
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!