1

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?

1
  • Before sending the data to the view check whether the wonCount value is set or not and change the load view line to $this->load->view('dashboard', $data); Commented May 10, 2018 at 7:02

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

It is still same didn't change anything
if no result, it's because COUNT(status) is 0
Another error occurred for return line. Message: Trying to get property of non-object. By the way I don't have to use array If it is easire I can use object.
I'm off for dinner, please check if there is no spelling error in your code. Also if following the link of CI docs you should be able to track down the error, use and abuse print_r($data) to control if the return is as desired.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.