Displaying multiple data with different date using PHP and Codeigniter - php

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

Related

Complicated condition of getting from database

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.

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

How to get single record if the multiple records are on single ID?

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

Get the sum of the colunm with specific id from one table and get the details from another table with that id in codeigniter

I want to get the sum of the column with specific id from one table and get the details from another table with that id with mysql query and codeigniter.
I have used this code:
function total_commision($ids)
{
$values=array();
for($o=0;$o<count($ids);$o++) {
$this->db->select('SUM(commision) AS total_commision', FALSE);
$this->db->where('hierarchy_users_id',$ids[$o]);
$query = $this->db->get('commision');
$value=$query->result()[0];
array_push($values, $value);
}
return $values;
}
I want to get the details of each member from the another table with that id in database using codeigniter query .
you can try the following
function total_commision($ids)
{
$query = $this->db
->select('SUM(commision) AS total_commision, admin_credentials.*', FALSE)
->from("commision")
->join("admin_credentials", "admin_credentials.id = commision.hierarchy_users_id", "left")
->where_in('commision.hierarchy_users_id', $ids)
->group_by('commision.hierarchy_users_id')
->get();
return $query->result();
}

Codeigniter selecting a certain table from database

I just want to ask if you can select a certain table in the database? There is this code in my
model:
public function select_tables(){
$tables = $this->db->list_tables();
return $tables;
}
and in my controller:
public function maintenance(){
$this->data['title'] = "Inventory Maintenance";
$this->load->vars($this->data);
$this->load->view('homeview');
$selecttable['tablename'] = $this->inventory_model->select_tables();
$this->load->view('maintenance_view', $selecttable);
$this->load->view('footer_view');
}
Here is the printscreen in my view:
There is the list of my tables in the database, what I want is I can only show limited tables, for example I just want to show "tbladditional, tblemployees, tblinventorytype". Is there a syntax where I can select a certain table? An equivalent to this syntax
"Select * from 'tablename' where 'tablename' = 'something'"
It's so confusing so please I really need your help. Thank you so much!!
public function select_tables(){
$tables = $this->db->list_tables();
$req_tables = array("tble1", "table2"); //pass your table names as Array
$tables = array_intersect($tables, $req_tables);
return $tables;
}
This is the programmatic way I can come with. As CI doesn't have any method to retrieve specific table names.
you can put a mixture of Codeigniter and the query result
For example:
$table_list = $this->db->list_tables();
foreach($table_list as $tl)
{
if(strpos(strtolower($tl),'tbladditional')==true ||
strpos(strtolower($tl),'tblemployees')==true ||
strpos(strtolower($tl),'tblinventorytype')==true)
{
$query = "Select * from $tl";
}
}

Categories