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)
{...}
Related
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);
I want get line_id from my lines_stations table:
This is timetable search so I have from form station_from_id and station_to_id.
My search function is here:
public function search($station_from_id, $station_to_id) {
$stations = array($station_from_id, $station_to_id);
$this->db->from('lines_stations');
$this->db->where_in('station_id', $stations);
$q = $this->db->get();
$lines_stations = $q->result();
return $q->result();
}
This return me all row from lines_stations where station_id is station_from_id or station_to_id but I need get only these where order is correctly - for example:
station_from_id = 2
station_to_id = 1
I should get only this:
I haven't idea how to do this.
I'm using CodeIgniter 3.
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();
}
In this image, 5 record are with id 80, but when i fetch them they all are coming but i want to show just one record only.
My rest code is below here
$data['query7'] = $this->ORB_Model->get_skilldash();
public function get_skilldash()
{
$this->load->database('orb');
//$this->db->distinct('master_id');
$query = $this->db->get_where('skills_tb', array('master_id' => $this->session->userdata('master_id')));
return $query->result();
}
Use $query->row() instead of $query->result();
public function get_skilldash()
{
$this->load->database('orb');
$master_id = $this->session->userdata('master_id');
$query = $this->db->get_where('skills_tb', array('master_id' =>$master_id));
return $query->row();
}
If you want a specific row returned you can submit the row number as a digit in the first parameter:
$row = $query->row(3);
Or Simply use it with $this->db->distinct(); do like this:
$this->load->database('orb');
$this->db->distinct();
$master_id = $this->session->userdata('master_id');
$query = $this->db->get_where('skills_tb', array('master_id' => $master_id));
return $query->row();
for more : https://www.codeigniter.com/user_guide/database/results.html#result-rows
I am filtering some data, I have a field called "faculty" and the options are "Professor, Associate Professor, Research Professor" ... so on. My filtering works for every other field but for this specific case I am having trouble because the professor word appears in all the data and as a result, I get all the data that matches the word "professor" so it is not filtering anything. How can I make it to search only for the specific word 'Professor' and avoid getting all the others (research professor, associate professor...)?
// My code
function get_search_filters($limit, $start, $search_faculty)
{
$this->db->order_by('lname', 'asc'); //order records by last name
$this->db->limit($limit, $start);
/* search keyword in all columns*/
$this->db->like('faculty', $search_faculty);
$query = $this->db->get('expertise');
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
$this->db->order_by('lname', 'asc'); //order records by last name
$this->db->limit($limit, $start);
$this->db->where('faculty', $search_faculty);
$query = $this->db->get('expertise');
https://www.codeigniter.com/userguide3/database/query_builder.html#looking-for-specific-data