here's my model code :
function total($id)
{
$this->db->select_sum('score');
$q = $this->db->get('my_table');
$this->db->where('id',$id);
$this->db->group_by('id');
return $q->row()->score;
}
why the output still sum all of row not the specific row with id?
$this->db->get() actually runs the query. You need to call that last.
function total($id)
{
$this->db->select_sum('score');
$this->db->where('id',$id);
$this->db->group_by('id');
$q = $this->db->get('my_table');
return $q->row()->score;
}
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();
}
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
public function countTasks(){
$q = $this->db->get_where('tasks', array('task_status' => 1));
return $this->db->count_all_results();
}
This is the function I have in my model that counts all rows in my 'tasks' table where the column 'task_status' is equal to 1. It basically returns an integer that I use for my pagination. The query is not working as it doesn't return any data even though I know I have 4 rows that match that requirement in my table.
I was gonna use the returned value for my pagination. Please help. Thanks
Try
return $q->num_rows();
Or
$this->db->where('task_status', '1');
$query = $this->db->get('tasks');
return $query->num_rows();
$this->db->count_all_results(); is not working after the get function, so do this instead:
public function countTasks(){
$q = $this->db->get_where('tasks', array('task_status' => 1));
return return $q->num_rows();
}
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 function that loads comments from a database.
in an other page,
I want to get the 3 last rows from a table in my database(so it would show the last 3 comments).
that is the function that i have in my model:
public function load3Comments(){
$this->db->order_by("c_id", "desc");
$query = $this->db->get('comment');
return $this->get_results($query);
}
public function load3Comments(){
$this->db->order_by("c_id", "desc");
$this->db->limit(3);
$query = $this->db->get('comment');
return $this->get_results($query);
}
That should do it. THere's a function for it in CI. http://ellislab.com/codeigniter/user-guide/database/active_record.html