so I'm having this issue not being able to get just a string from a table. This is my method in the controller:
public function get_tree($id){
$data['tree'] = $this->Product_model->get_tree($id);
return $data;
}
And this is the function in the model:
function get_tree($id){
$this->db->select('ascending_path');
$this->db->from('category');
$this->db->where('id', $id);
$result = $this->db->get();
return $result;
}
I'm showing this with Ajax into the view but nothin seems to show up. There is not even an error, 200 status code and the request is shown in the access log. Any hints?
PS: if I try to json_encode it and then pass the dataType:json in the ajax call all it comes back is:
{"tree":{"conn_id":{"affected_rows":null,"client_info":null,"client_version":null,"connect_errno":null,"connect_error":null,"errno":null,"error":null,"error_list":null,"field_count":null,"host_info":null,"info":null,"insert_id":null,"server_info":null,"server_version":null,"stat":null,"sqlstate":null,"protocol_version":null,"thread_id":null,"warning_count":null},"result_id":{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null},"result_array":[],"result_object":[],"custom_result_object":[],"current_row":0,"num_rows":null,"row_data":null}}
Everything seems to be null. I don't even know what is that object, I mean, the query is supposed to bring just one string.
Change return $result;
to return $result->result();
Read more about Generating Query Results here
use this
function get_tree($id){
$query = $this->db->query("SELECT ascending_path FROM category WHERE id ='$id'");
$result = $query->result_array();
return $result;
}
Related
I have the following codes in my model.
This is my original code.
I have tested that the value of my $category_id = 1
function getSubCategory($category_id){
$this->db->select('id, subcategory');
$this->db->from('subcategory');
$this->db->where('category_id', $category_id);
$this->db->order_by("subcategory", "asc");
$query = $this->db->get();
return $query->result();
}
however I am encountering a problem where $this-db->where is not working.
function getSubCategory($category_id){
$cat = 1;
$this->db->select('id, subcategory');
$this->db->from('subcategory');
$this->db->where('category_id', $cat);
$this->db->order_by("subcategory", "asc");
$query = $this->db->get();
return $query->result();
}
I tried this line of code just to make sure that I am not doing any syntax error. And with these line of codes I am getting what I wanted. I wonder what makes my original code not work.
It would be a great help if someone can help. Thanks.
It turns out that the problem I was having is from the AJAX function that I was calling to the call the method in my model.
The answer is seen here
AJAX not displaying result
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;
}
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 am trying to write a CSV in Codeignter for a result set fetched from db.
I am already using the solution mentioned here. Reports in Codeigniter
But the issue is that it writes the whole table. Now I want to fetch and write specific records. So here is my model code.
$where = "hangtag_spoc_number = 2202";
$result = $this->db->select('*')
->from('hangtag_request')
->where($where)
->get()
->result_array();
return $result;
It gives me this error.
You must submit a valid result object
If I change the model code to this
return $query = $this->db->get('hangtag_request');
It works perfectly. Is there any way I can make my model code to return the results in the form of DB object? Coz it seems thats the form we need.
Thanks.
If you need result object then you shouldn't use the result_array()
$this->db->get();
The above statement runs the selection query and returns the result.It does return in form that can be used for presentation.
result_array()
This function returns the query result as an array, or an empty array when no result is produced.
Its good to use return $query = $this->db->get('hangtag_request'); for obtaining result object.
I got it solved.
Just had to remove result_array() so the correct code becomes
$where = "hangtag_spoc_number = 2202";
$result = $this->db->select('*')
->from('hangtag_request')
->where($where)
->get();
return $result;
This is probably really simple, but I just started using Code Igniter.
I have the following code in a model and want to get the database query array to pass to the controller and then pass it to a page view.
public function view_user($id){
$this->db->where('userid', $id);
$query = $this->db->get('users');
return $query->result_array();
}
What is the code necessary to get the returned array in the controller?
Thanks in advance for your help!
here you Go with perfact answer
Model code
public function view_user($table,$id)
{
$this->db->where('userid', $id);
return $this->db->get($table);
}
Now in controller
Call your Method Like
$this->load->model('model_name');
$data['array_name']= $this->model_name->view_user('Tabelname',$id)->result();
If u want to Get Only single row Insted of result write ->row(); in above statement
now load your view and pass the $data as 2nd pararameter
$this->load->view('viewname',$data)
now in your VIEW
Acesss Array Like
<?php foreach($array_name as $row)?>