5

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

enter image description here

sooraj s pillai
  • 866
  • 2
  • 17
  • 39

5 Answers5

4

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.

  • Never mind, if you have another problem about this, maybe I can help you. :) –  Dec 21 '17 at 08:10
1
$query =$this->db->query("SELECT COUNT(`product_id`) AS count  FROM `cart` WHERE 
`username`='$sess'");
Haziq Ahmed
  • 87
  • 1
  • 6
0

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?>
Arun Kumaresh
  • 6,211
  • 6
  • 32
  • 50
0

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.

DeadCoderz
  • 237
  • 3
  • 8
0

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);
}
Riyenz
  • 2,498
  • 2
  • 9
  • 23