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

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;

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

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

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

codeigniter select not working

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

CodeIgniter - Pass database array from model to controller and then view

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)?>

Categories