Print count value in codeigniter - php

I want to print count of some records in my project , i tried using some code but no result is giving can anyone figure out the mistake please.
controller
function cart_count()
{
$sess = $this->session->userdata('SESS_USER');
$query = $this->product_model->c_count($sess);
$data['count'] = $query->result();
$query = $this->db->get("cart");
$data['records'] = $query->result();
$this->load->view('frontend/menu',$data);
}
Model
public function c_count($sess)
{
$query =$this->db->query("SELECT COUNT(`product_id`) FROM `cart` WHERE `username`='$sess'");
return $query;
}
View
<?php foreach($count as $count){echo $count;}?>

I see your query using count and where. That is mean you just select 1 row of data like this.
username COUNT(product_id)
admin 3
The return data is just 1 row, so you can return the data using row() like this return $query->row().
Model : return your data default as a row() for 1 row of data.
public function c_count($sess)
{
$query = $this->db->query("SELECT COUNT(product_id) as count_id
FROM cart
WHERE username = '$sess'");
return $query->row();
}
Controller : Call your data here.
function cart_count()
{
$sess = $this->session->userdata('SESS_USER');
$query = $this->product_model->c_count($sess);
$data['count'] = $query->count_id; // CHANGE FROM $data['count'] = $query->result();
// If you dont mind, I change your code :
// $query = $this->db->get("cart");
// $data['records'] = $query->result();
$record = $this->db->get("cart");
$data['records'] = $record->result();
$this->load->view('frontend/menu',$data);
}
Views Here is how to call your data, ill give example using <span>.
<span>Admin total product : <?php echo $count; ?> Products</span>
There is so many ways to call returned data from database.
You can also use <?php echo $query->count_id; ?> in your views without set it into $data['count'] in your controller. You can try it now. :) Hope this help.
Note : If you want to call more than 1 data, dont use where but use a group by. I want to give you an example for that, but it's a different problem with your question. :) and if there any typos, please let me know and I will fix it.

$query =$this->db->query("SELECT COUNT(`product_id`) AS count FROM `cart` WHERE
`username`='$sess'");

change the query to
$query =$this->db->query("SELECT COUNT(`product_id`) as count FROM `cart` WHERE `username`='$sess'");
$query->result() will return array of objects
in view you will get as object you can use
<?php foreach($count as $count){echo $count->count;}?>
or
<?php echo $count[0]->count?>

The issue is with your model class where you fetch the number of row counts.
Actually, in CodeIgniter the result set fetched matches with what the columns of DB tables are.For eg. the statement
$query =$this->db->query("SELECT COUNT(`product_id`) FROM `cart` WHERE `username`='$sess'");
will return a result set something like this
Array ( [0] => stdClass Object ( [COUNT(`product_id`)] => 60 ) )
And when you try to display the result with this line <?php foreach($count as $count){echo $count;}?>
you get error because you are asking to show $count data variable of $count array which is not present.
One simple trick to solve this problem without much changes in your code is to use alias in your query.Just change your query to this
$query =$this->db->query("SELECT COUNT(`product_id`) as 'nums' FROM `products` WHERE `service_id`='$sess'");
And fetch the result in the view as <?php foreach($count as $c){echo $c->nums;}?>
However,in my opinion its better to use inbuilt function num_rows() of CI for this.

Simply use PHP count() function after getting the result
CONTROLLER
function cart_count()
{
$sess = $this->session->userdata('SESS_USER');
$query = $this->product_model->c_count($sess);
$data['count'] = count($query->result());
$query = $this->db->get("cart");
$data['records'] = $query->result();
$this->load->view('frontend/menu',$data);
}

Related

get num_rows and result() data in same function on codeigniter

hı,
how can ı return get result data and result num rows in same functions
ı try with this function but it is not run
$user_id=$this->session->user_sess['id'];
$this->db->select('*');
$this->db->where('uto', $user_id);
$this->db->where('isread','0');
$this->db->from('user_messages');
$query=$this->db->get();
$q['id']=$query->result();
$q['data']=$query->num_rows();
return $q;
$user_id=$this->session->user_sess['id'];
$this->db->select('*');
$this->db->where('uto', $user_id);
$this->db->where('isread','0');
$this->db->from('user_messages');
$query=$this->db->get();
$q['result']=$query->result();
$q['rows']=$query->num_rows();
return $q;
controller:
$result = $this->some_model->my_model_function();
$num_rows = $result['rows'];
$data = $result['result'];
Try this one out...
$user_id=$this->session->user_sess['id'];
$query= $this->db->query("SELECT * from user_messages WHERE uto = '".$user_id."' AND isread = '0' ");
return $query->num_rows();
hope that helps.
you may try with storing result set into two variable like : -
$query=$query1= $this->db->select('*')->from('table_name')->get();
echo $query->num_rows(); //number of rows
print_r($query1->result()); // result object
second approach by using result set object itself like :-
$query=$this->db->select('*')->from('table_name')->get();
echo $query->result_id->num_rows; //number of rows
print_r($query->result()); // result object

how call a result from database count(*) on CI

i got code like this on codeigniter :
<?php
$query = $this->db->query("SELECT count(*) FROM data_pribadi WHERE STATUS = 'diterima'");
return $query->result();
?>
how can i call a result?
Try this in your model.
$this->db->select('*');
$this->db->from('data_pribadi');
$this->db->where('STATUS','diterima');
$query = $this->db->get();
return $result=$query->result();
And in your Controller, you can seek the results to your view like the below code
$this->load->model('Mymodel');
$this->load->view('YourviewName',array($data=>$this->Mymodel->Myfunction()));
And these also for your view (example)
<?php
foreach ($data as $u):
echo"<option value ='$u->column_name1'>".$u->column_name. "</option>";
endforeach;
?>
Your code should be placed on model.
Let me guide you.
MODEL:
// file name: Mymodel.php
function test() {
$query = "SELECT count(*) AS total_acc FROM data_pribadi WHERE STATUS = 'diterima'";
return $this->db->query( $query )->row()->total_acc;
}
How about the result?
CONTROLLER
$this->load->model( 'mymodel' ); //available in CI3
$total_acc = $this->mymodel->test();
echo $total_acc;
Done, now you get it. Because your request only produces one row, I think it's suitable for you.
Btw, salam kenal :)

Get All Indexes Of array in codeigniter

Here Is MY Output OF problem i want to get all reciver_id indexes from this array and after getting all indexes i want to get the result from another table users against these id's
$data['cat_row'] = $this->messages_model->get_sentnotify($user_id);
foreach($data['cat_row'] as $key){
$profile = $key['reciver_id'];
}
$data['profile'] = $this->messages_model->Sender_profile($profile);
through this code i get only 1 value of first index how to get all...
getting all i want to get users from other table against each id
function Sender_profile($profile)
{
$this->db->where('id', $profile);
$query = $this->db->get($this->db_table2);
return $query->result_array();
}
kindly help me about sql also how i get all values from database in one iteration
Code Part 1
Get all reciver_id in an array like follows:
$data['cat_row'] = $this->messages_model->get_sentnotify($user_id);
$profile = [];
foreach($data['cat_row'] as $key){
array_push($profile, $key['reciver_id']);
}
$data['profile'] = $this->messages_model->Sender_profile($profile);
Code Part 2
Use Codeigniter's where_in() function.
function Sender_profile($profile) {
$this->db->where_in('id', $profile);
$query = $this->db->get($this->db_table2);
return $query->result_array();
}
In your controller , you need to access all your receiver_id then modify your foreach loop:
$i=0;
foreach($data['cat_row'] as $key){
$profile[$i] = $key['reciver_id'];
$i++;
}
Now you have all your receiver_id in $profile array. and pass this array in controller function and should query like this:
SELECT * FROM table WHERE id IN (5,4,3,1,6);// Just for example you need to modify your query according to your need.

return the columns of a result row as an array that can be referenced by index in codeigniter / active record

I am getting a specific row in a database with codeigniter / php /active record. I a would like to return the result as an array of the row's columns that can be referenced like this:
result[0] = column1
result[1] = column2
...
This is my current query:
public function get_instance($instanceID = NULL){
$this->db->select('organism, feature, goal');
$this->db->from('extra_instances');
$this->db->where('id', $instanceID);
$query = $this->db->get();
return $query->row_array();
}
$data['instance'] = $this->instances_model->get_instance($instanceID);
But currently, to echo these, I (think) I need to give the name of the column, like:
<?php echo($instance['goal']) ; ?>
Is there a way to do this so I can say:
<?php echo($instance[2]) ; ?>
You can do this by create a new array and assigns all values of old array to new array.
You can use the array_values() function to do this.
Here is the example. so you can get more idea.
public function get_instance($instanceID = NULL){
$this->db->select('organism, feature, goal');
$this->db->from('extra_instances');
$this->db->where('id', $instanceID);
$query = $this->db->get();
$results = $query->row_array();
return array_values($results);
}

codeigniter single result without foreach loop

I am using codeigniter to get a single result from the db, like this:
$user = $this->db->query("SELECT * FROM users LIMIT 1");
I am then getting the returned object and looping trough it, this way:
foreach ($user->result() as $row) { ... }
As you can imagine, there is not point in using the loop, since there is only one result. Is there a way to get users parameters without looping? Something among this line:
echo $user['name'];
Thanks in advance.
$user = $this->db->query("SELECT * FROM users LIMIT 1")->row();
for having it as an object ( echo $user->name;)
$user = $this->db->query("SELECT * FROM users LIMIT 1")->row_array();
for having is as an array ( echo $user['name']; );
$user = $this->db->limit(1)->get('users')->row(); //or ->row_array();
using AR;
$row = $user->first_row();
to get the first row out of a result set ($this->db->result()), as an object (default);
$row = $user->first_row('array');
to get the first row out of a result set, as an array;
$row = $user->row_array(1);
to return a specific row (first, in this case. Just pass the <N> of the row). Works also for $user->row(1);
It would be:
$user = $this->db->query("SELECT * FROM users LIMIT 1")->row_array();
More details here: http://ellislab.com/codeigniter/user-guide/database/results.html
do something like this
//in the model
$query = $this->db->query("your query");
$row = $query->row();
if (isset($row))
return $row;
}else{
return false;
}
then echo your results like this
echo $row->username;
if you want to pass it to a view
//in the controller
$this->load->model('somemodel');
$hm=$this->somemodel->somemethod();
$data['query']=$hm;
$this->load->view('path/to/view',$data);
Then echo the same way
echo $row->username;
Suppose for example your model function is like this.......
public function get_users()
{
$query = "Select * from users limit 1";
$res=$this->db->query($query);
if($res->num_rows()>0){
return $res->result("array");
}
return array();
}
Suppose you are calling this model in your controller function like this
$users = $this->model_file->get_users();
then you can echo like this echo $users[0]['user_name'];

Categories