Call to a member function result() on array in codeigniter - php

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

Related

Codeigniter - How to access the result_array()

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

Why doesn't CI convert count statement to an array?

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;

count() parameter must be an array or an object when there is no record in the database

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 : [];

what's wrong with my sum query in model php codeigniter?

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

Only last row getting displayed in view in Codeigniter

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. :)

Categories