FlyingMadman
FlyingMadman

Reputation: 29

CodeIgniter Display MySQL Count

View

<h3><?php echo $wonCount; ?></h3>

Controller

$this->load->model('coupon_model');
$data['wonCount'] = $this->coupon_model->wonCount();
$this->loadPage('dashboard', $data);

Model

public function wonCount() {

    return $this->db->query("SELECT COUNT(status) FROM coupon WHERE status = 'won'");
}

Error :

Severity: Notice

Message: Undefined variable: wonCount

How can I display count?

Upvotes: 1

Views: 63

Answers (1)

Vickel
Vickel

Reputation: 7997

you need to send a query result (you can use row() in case you only expect one row to be returned, like in your example) back to the controller and use an alias in your query:

so write in your model something like:

$query = $this->db->query("SELECT COUNT(status) as my_count FROM coupon WHERE status = 'won'");
return ($query->num_rows() ) ?$query->row()->my_count:false;    

now, with your code example in controller and view, the variable $wonCount will echo out in your view correctly

more on Generating Query Result Rows here

Upvotes: 1

Related Questions