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.
Related
I want to get data from two tables, the second table is for rating so i want to get rating of products simultaneosly. Below code is not working for me if i change
$this->db->select('dg_products.',', AVG(dg_rating.rating) As
averageRating');
to
$this->db->select('*');
then it is working.
Please help to sort out my issue.
public function get_rating()
{
$this->db->select('dg_products.*','*, AVG(`dg_rating.rating`) As averageRating');
$this->db->from('dg_products');
$this->db->join('dg_rating', 'dg_products.id = dg_rating.product_id','left');
$this->db->where('dg_products.is_featured_prod','1');
$this->db->group_by("dg_products.id");
$query = $this->db->get();
$result = $query->result();
return $result;
}
Try it like this :
$this->db->select('dg_products.*, AVG(`dg_rating.rating`) As averageRating');
you just have an unneeded quotes in there.
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 have a problem on getting multiple data of a single patient. I can only generate 1 physical examination result and I can't get all of it.
My Controller
public function print_sum_report($case_id){
//fetching all data from different tables
$case_id = 1;
$postnatal_id = 1;
$this->load->model('Prms_model');
$data['n_status'] = $this->Prms_model->get_status_f($case_id);
$data['n_mh'] = $this->Prms_model->get_mh_f($case_id);
$data['n_pe'] = $this->Prms_model->get_pe_f($case_id);
$data['n_post'] = $this->Prms_model->get_pn_f($postnatal_id);
$data['n_infant'] = $this->Prms_model->get_infant_f($case_id);
$this->load->view('report/reportsum', $data);
// print_r($data);
}
My Model
public function get_pe_f($case_id){
// joining 2 tables (physicalexamination and patient_info by ID)
$this->db->select('*');
$this->db->from('physicalexamination');
$this->db->where('case_id', $case_id);
$this->db->join('patient_info', 'patient_info.patient_ID = physicalexamination.Patient_ID');
$query = $this->db->get();
return $query->result();
}
$data['n_pe'] is a result object which contains many rows. You need to iterate over it to gather all the rows:
foreach($data['n_pe'] as $pe) {
var_dump($pe);
}
Also, you could write your model this way:
public function get_pe_f($case_id) {
// joining 2 tables (physicalexamination and patient_info by ID)
return $this->db
->where('case_id', $case_id)
->join('patient_info', 'patient_info.patient_ID = physicalexamination.Patient_ID');
->get('physicalexamination')
->result();
}
In my CodeIgniter project I'm getting the list of projects and successfully output them on a page. However, the data in one of the columns on that page should be retrieved from a different table in DB using the project ID. Could anybody help me to figure out how that can be done? Basically I need to make another query to that other table specifying the project id but don't actually know how to do that with CodeIgniter.
UPDATE
In the model I'm getting the list of projects with the following function:
function get_projects_list($page, $limit){
$sql = sprintf("SELECT * FROM Project WHERE deleted != 1 LIMIT %d, %d", ($page-1)*$limit, $limit);
$query = $this->db->query($sql);
return $query->result();
}
And in the controller I call the following function:
$projects_list = $this->Project_management_model->get_projects_list($curPage, self::$LIMIT_PER_PAGE);
$data['projects_list'] = $projects_list;
$data['cur_page'] = $curPage;
$data['page_count'] = $pageCount;
$this->load->view('project_management_view', $data);
And in the view I simply run on the $data with foreach and list the results in a table. In that table there's a column where I need to show a result from another table based on the ID of the project of that very row.
Thanks for helping.
You didn't mention whether you are using ActiveRecord or not. I am assuming that you are. I'll also guess that maybe what you need to do is use a JOIN.
If you were using straight SQL, you would do this using some SQL that might look something like this:
SELECT a.appointment_time, u.user_real_name FROM appointment a, site_user u WHERE u.site_user_id = a.user_id;
That would pull the user's name from the user table based on the user id in the appointment table and put it with the appointment time in the query results.
Using ActiveRecord, you would do something like this:
$this->db->select('appointment_time,user_real_name')->from('appointment')->join('site_user', 'site_user_id=appointment_user_id');
But why don't you tell us a little bit more about your question. Specifically, do you want this column in the other table to be related to the rows from the first table? If so, my JOIN suggestion is what you need.
I've actually found a way to do that with a custom helper. Creating a new helper and loading it in the controller gives an option to use the function from that helper in the view.
Thanks.
public function get data()
{
$this->db->flush_cache();
$query = $this->db->get('project_table');
$result = $query->result();
$data = array();
for ($i = 0;$i < count($result);$i++)
{
$data[$i] = $result[$i];
$data[$i]['project_data'] = temp($result[$i]->id);
}
return data;
}
private function temp($id = 0)
{
$this->db->flush_cache();
$this->where('id',$id);
$query = $this->db->get('project_table2');
$result = $query->result();
if (count($result) != 0)
return $result[0]->data;
}
you can do it by some thing like that,or you can use sub-query by query function of database.