I'm just wondering how can I create a page with the URL on fetched data.
For example
chapter.php?bookid=135&chapter=Chapter_1
So what I need is to create a page with the data (from other tables) based on the bookid and chapter.
If you need to get database data for book
http://www.codeigniter.com/user_guide/database/query_builder.html#selecting-data
public function get_data() {
$this->db->select('*');
$this->db->from('yourtablename');
$this->db->where('bookid', $this->input->get('bookid'));
$query = $this->db->get();
return $query->row();
}
Then on controller
$book_result = $this->model_book->get_data();
$data['bookname'] = $book_result->bookname;
$this->load->view('some_view', $data);
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 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();
}
I am developing а website for online shopping using CodeIgniter framework.
I am able to fetch a category, and when I click on a category id it will redirect to another page where I want to display all the data from the database related to that category.
How can I fetch data from database using CodeIgniter framework?
To fetch data form database in codeigniter
1. Controllers query
public function getdata()
{
$this->load->model('insert_model');
$data['result'] = $this->my_model->fetch_data($id);
echo "<pre>";
preint_r($data['result']); // database return value from model
}
2. Models query
function fetch_data($id=null)
{
$this->db->select('*');
$this->db->from('mytbl'); // mytbl is database table name
$this->db->where('id',$id);
$query = $this->db->get();
$result = $query->result_array(); // this query fetch result in array form
if(!empty($result))
{
return $result ;
}
}
Another way to fetch data from database
$query = $this->db->query("select * from mytbl");
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
echo $row->title;
echo $row->name;
echo $row->body;
}
}
I'm trying to fetch certain values from and then pass it to another model in the same control.
However I'm only able to display the last row in the view.
I have shared my code below and I'm not sure where I'm going wrong.
Controller:
public function test($id){
$mapping_details = $this->queue_model->get_mapping_details($id);
foreach ($mapping_details as $value) {
$data['agent_details'] = array($this->agent_model->get_agent_details($value['user_id']));
}
$this->load->view('app/admin_console/agent_queue_mapping_view', $data);
}
Model:
public function get_agent_details($id) {
$query = "select * from user_table where id = ".$id." and company_id = ".$this->session->userdata('user_comp_id');
$res = $this->db->query($query);
return $res->result_array();
}
Welcome to StackOverflow. The problem is the iteration in your controller. You are iterating through the $mapping_details results and per every iteration you are re-assigning the value to $data['agent_details'] , thus losing the last stored information. What you need to do is push to an array, like this:
foreach ($mapping_details as $value) {
$data['agent_details'][] = $this->agent_model->get_agent_details($value['user_id']);
}
However, wouldn't it be best if you created a query that uses JOIN to get the related information from the database? This will be a more efficient way of creating your query, and will stop you from iterating and calling that get_agent_details() over and over again. Think of speed. To do this, you would create a model method that looks something like this (this is just an example):
public function get_mapping_details_with_users($id){
$this->db->select('*');
$this->db->from('mapping_details_table as m');
$this->db->join('user_table as u', 'u.id=m.user_id');
$this->db->where('m.id', $id);
$this->db->where('u.company_id', $this->session->userdata('user_comp_id'));
return $this->db->get()->result();
}
Then your controller will only need to get that model result and send it to the view:
public function test($id){
$data['details_w_users'] = $this->queue_model->get_mapping_details_with_users($id);
$this->load->view('app/admin_console/agent_queue_mapping_view', $data);
}
Hope this helps. :)
i have a page that shows all student profiles with their results. all the user info i putted in the table "user"
and then i have another table "results" that shows all the students their scores from diffrent courses. the thing is i don't know how to write the query or controller function to link the student with their corresponding results. I need some help here thanks
Controller
function students()
{
$data = array();
$this->load->model('kdg_model');
$query = $this->kdg_model->get_students();
$query2 = $this->kdg_model->get_resultStudent();
if ($query)
{
$data['user'] = $query;
$data['results'] = $query2;
}
$this->load->view('students_view',$data);
}
Model
get_students get all rows from the database table user.
get_resultstudent gets all the rows from results. tried to combine them but it just gives me all the same rows back on every profile.
function get_students(){
$this->db->where('admin', 0);
$query = $this->db->get('user');
return $query->result();
}
function get_resultStudent(){
$this->db->select('*');
$this->db->from('results');
$this->db->join('user', 'user.id_user = results.FK_student');
$query = $this->db->get();
return $query->result();
}
I'm not sure what your database schema looks like but from what I can work out I've got this. Okay so in your controller you want to build your array of students. Let do this by calling two model functions. The first model function we are going to call we get us all of the students in the 'user' table. The model will pass back an array to the controller which we can loop through to get the individual students. On each iteration of the loop we can pass the 'id_user' to another function in our model to get the results for that student.
//This is where we will store the students and their results
$aData['aStudentResults'] = array();
//Load the model we need
$this->load->model('kdg_model');
//Get an array of students
$aStudents = $this->kdg_model->get_students();
//Now we have an array of student id's let loop through these and for each student
//let get their results
foreach($aStudents as $student)
{
//Add the student to the array
//The student id (id_user) is the key
$aData['aStudentResults'][$student->id_user] = $this->kdg_model->get_resultStudent($student->id_user);
}
//Pass out array of students to the view
$this->load->view('students_view', $aData);
These are the model functions we are calling in the controller
//Get all the students that are not admins
function get_students(){
$this->select('id_user')
-from('user')
->where('admin', 0);
$query = $this->db->get();
return $query->result();
}
//Use the id_user we were passed by the controller to get the results
function get_resultStudent($id_user){
$this->db->select('*');
$this->db->from('results');
$this->db->where('FK_student', $id_user);
$query = $this->db->get();
return $query->result_array();
}
At this point we now have all the data we need. We just need to pass it from the controlle to the view which we did by passing the view $aData. In our view we can access what we passed to it like so
<? foreach($aStudentResults as $studentId => $aResults): ?>
<p>Student id: <?= $studentId ?></p>
<?foreach($aResults as $aResult): ?>
//Do something with results
<? endforeach; ?>
<? endforeach; ?>
This hasn't been tested so there may be some syntax errors but hopefully you should get an idea of what I am trying to do and what you need to do.
It's important for you to understand how MVC works. Practice selected data from your database in your models, and then passing that data to your views via the controller. You'll quickly get the hang of it.
If there is anything you don't understand in this answer please leave a comment and let me know.