Print counter value in codeigniter

I want to print the number of some entries in my project, I tried to use some kind of code, but not a single result gives, can someone understand the error, 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;}?> 

enter image description here

+5
source share
5 answers

I see your request with count and where . This means that you simply select one row of data like this.

 username COUNT(product_id) admin 3 

The returned data is just 1 row, so you can return the data with row() , like this return $query->row() .

Model : return your default data as 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 up 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's how to name your data, for example, use <span> .

 <span>Admin total product : <?php echo $count; ?> Products</span> 

There are so many ways to call returned data from a database.
You can also use <?php echo $query->count_id; ?> <?php echo $query->count_id; ?> in their views without setting it to $data['count'] in their controller. You can try it now. :) Hope this help.

Note. If you want to call more than 1 data, do not use where , but use group by . I want to give you an example for this, but this is another problem with your question. :) and if there are any typos, please let me know and I will fix it.

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

change the request to

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

$query->result() will return an array of objects

you will get an object that you can use

 <?php foreach($count as $count){echo $count->count;}?> 

or

 <?php echo $count[0]->count?> 
0
source

The problem is in your model class, where you select the number of rows. In fact, in CodeIgniter, the result set selects matches with what the columns of the DB tables are. For example, for example. 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 using this line <?php foreach($count as $count){echo $count;}?> You get an error because you are asking to show the counter data variable $ count array, which is not there. One simple trick to solve this problem without significant changes in your code is to use an alias in your request. Just change your request to this

 $query =$this->db->query("SELECT COUNT(`product_id`) as 'nums' FROM `products` WHERE `service_id`='$sess'"); 

And get the result in the view as <?php foreach($count as $c){echo $c->nums;}?>

However, in my opinion, it is better to use the built-in num_rows() function for CI.

0
source

Just use the 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); } 
0
source

Source: https://habr.com/ru/post/1274242/


All Articles