I have a method in my controller as follows:
function surcharge_load_data(){
$data = $this->Surcharge_model->surcharge_load_data();
foreach($data->result_array() as $r) {
echo $r->surcharge_id;
}
}
The Surcharge_model method returns a result array. This is my method in the model:
function surcharge_load_data(){
$this->db->order_by('surcharge_id', 'DESC');
$query = $this->db->get('surcharges');
return $query->result_array();
}
The data returned from the above method looks like this:
[{"surcharge_id":"37","surcharge_curr":"EUR","surcharge_name":"MNM","surcharge_unit_id":"3","surcharge_value":"43","surcharge_category_id":"3","created_timestamp":"2019-06-03 06:18:18","updated_timestamp":"2019-09-01 08:19:18"},
{"surcharge_id":"36","surcharge_curr":"EUR","surcharge_name":"TOY","surcharge_unit_id":"3","surcharge_value":"433","surcharge_category_id":"3","created_timestamp":"2019-07-09 09:21:21","updated_timestamp":"2019-09-10 09:21:21"}]
A few questions as follows:
(1) The entire data is encapsulated in []. Does that mean it has returned one large object with array of arrays?
(2) Why do I get an error, Call to a member function result_array() on array? I thought $data is a result array. How can I access it please?
Try this, no need to use result_array() here
function surcharge_load_data(){
$data = $this->Surcharge_model->surcharge_load_data();
foreach($data as $r) {//changes
echo $r['surcharge_id'];
}
}
HERE, you are using result_array() so you need to use each object like $r['surcharge_id'] if you use result() then you need to use $r->surcharge_id
From your controller you can't retrieve the result_array method because this only exists within your Model.
So, in your controller you can read the answer from your model like:
function surcharge_load_data(){
$data = $this->Surcharge_model->surcharge_load_data();
foreach($data as $r) {
//just here you can retrieve your data
...
}
}
Where $data is the result from the model (before returned as $query->result_array()) which your are recieving in your controller
Related
I'm trying to make it so that you can view the number of rows of something on my website, I converted the result into an array and then foreached it in the view, yet it calls the argument invalid.
As I said, I've tried the usual, passing on the data from the model to the controller and then to the view. I tried returning $query->num_rows(); as well as a count_all_results back to the controller array for it to be used in the view, both are invalid.
controller.php
$this->load->model('Main');
$data = array(
'getapps' => $this->Main->countapps()
);
$this->load->view('header', $data);
view.php
<?php foreach($getapps as $apps) {
?>
apps <strong><?php echo $apps; ?></strong>
<?php }
?>
model.php
public function countapps() {
$this->db->where('usedby', $_SESSION['uid']);
$this->db->get_where('apps', 'appstatus', 'Used');
$stmt = $this->db->count_all_results();
return $stmt;
}
What I expect to happen is for the view to display the number of rows, instead it tells me that the foreach argument is invalidly supplied.
In CI $this->db->count_all_results() substitutes $this->db->get() in a database query. Therefore you cannot use $this->db->get_where() and $this->db->count_all_results() at the same time, use num_rows() instead.
write your model function like this:
public function countapps() {
$query=$this->db->where('usedby', $_SESSION['uid'])
->get_where('apps', 'appstatus', 'Used');
$stmt = $query->num_rows();
return $stmt;
}
you could change in your controller to a shorter:
$data['getapps'] => $this->Main->countapps();
Then in your view, you don't need to use foreach() as $data['getapps'] is not an array but a number. You can output it in the view simply like
echo $getapps;
I am stuck in getting a value from model and process it in controller. I am new to codeigniter. My model code is this:
public function display_cat_courses($value)
{
$this->db->select('*');
$this->db->from('courses_tbl');
$this->db->join('course_category_tbl', 'courses_tbl.course_category_id = course_category_tbl.id');
$this->db->where('course_category_id', $value);
$query = $this->db->get();
return $query->result();
}
And my controller is this:
public function view()
{
$this->load->model('Category_model');
$cat_id = $this->uri->segment(3);
$data['category']=$this->Category_model->display_cat_courses($cat_id);
}
I just want to get a column value from course_category_tbl and store that inside an array as array element to process that later.Model works fine but in my controller I want to store "course_type" which is a column in course_category_tbl. How to get it.
As you are succesfully getting all datas in array format from model in controller and you only want one field data from array that you need to use for some purpose.For that,you can make use of
$field_values_array = array_column($whole_array, 'field_name');
print_r($field_values_array );
From this code,you will get all values of that column field in array Format,and later you can pass it to view for further proccessing.
You can select required column only:
$this->db->select('course_category_tbl.course_type');
If there is only one row that you'll get by passing the category ID then in the model in place of $query = $this->db->get(); you can add put
$this->db->row_array() and this will give you a one dimensional array and then from the result $query you can access the desired elemnt by passign the index.
E.g
$query['course_type'] will give you course type
I am using Codeignator, I have a function in the model which is
function getList()
{
$result = $this->db->where(['members.member_type'=>1,'members.is_Approved'=>1,'members.is_status'=>1])
->order_by("members.member_id", "desc")
->from('members')
->join('admin_users','admin_users.admin_user_id = members.created_by')
->get()
->result();
if($result)
{
return $result;
}
else
{
return 0;
}
}
Above code, I am using to display the all the list on view page which is working. Now I have one more view page and I am displaying the total number of the users using the same code. So I added the code
$data['primary_data'] = count($this->List_model->getList());
print_r($data['primary_data']);
It's also working but when there are no records then I am getting the error
count() parameter must be an array or an object that implements countable in codeigniter
Would you help me out in this?
CodeIgniter's $this->db->result() method returns the query result as an array of objects, or an empty array on failure.
Just return the result as it is. If there are results it will return an array of objects. If no result found, it will return an empty array. So the counting on the result will work all the time.
function getList() {
return $this->db->where(['members.member_type'=>1,'members.is_Approved'=>1,'members.is_status'=>1])
->order_by("members.member_id", "desc")
->from('members')
->join('admin_users','admin_users.admin_user_id = members.created_by')
->get()
->result();
}
You can return empty array instead of 0 in getList() method.
return [];
can write this:
return $result ? $result : [];
Controller Code:
$last['row']=$this->assign_model->get_last_row();
foreach($last->result() as $issue)
{
$id=$issue->id;
$at=$issue->time;
}
Model Code:
public function get_last_row()
{
$query = $this->db->query("SELECT * FROM issues ORDER BY id DESC LIMIT 1")->row_array();
return $query;
}
While I am executing this it will displays:
Message: Call to a member function result() on array
Can anyone help to correct this code??
you are fetching single row then what is the use of foreach here
$last = $this->assign_model->get_last_row();
And simply println $last variable you can get record of last row.
Replace this to
foreach($last->result() as $issue)
this
foreach($last['row'] as $issue) # or assign it with variable
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)?>