0

I'm learning codeigniter framework. I've managed to make login page work, now I'm trying to pass data from controller to my view. I'm getting errors I have looked and looked here and did as suggested still getting the errors/

Model

class Dashboard_Model extends CI_Model{ function __construct(){ parent::__construct(); } public function getTasks(){ $this->db->Select("COUNT(taskID) as totaltasks"); $this->db->from('tasks'); $query = $this->db->get(); return $query->result(); } } 

Controller

class Dashboard extends CI_Controller { function __construct(){ parent::__construct(); $this->load->model('admin/dashboard_model'); } public function index(){ if(!$this->session->logged_in){ redirect('user_login'); }else{ $data['title'] = $this->session->fullname. " | Dashboard"; $this->load->view('admin/common/header',$data); $data['total'] = $this->dashboard_model->getTasks(); $this->load->view('admin/dashboard',$data); $this->load->view('admin/common/footer'); } } } 

View

 <h2 class="text-white"><span data-plugin="counterup"><?php echo $total?></span> <small><i class="mdi mdi-arrow-up text-success"></i></small></h2> 

Error :

Severity: Notice

Message: Array to string conversion

Filename: admin/dashboard.php

Line Number: 40

What I am doing wrong?

6
  • $this->dashboard_model->getTasks(); retunrs array not string Commented Nov 21, 2018 at 12:04
  • $this->dashboard_model->getTasks() return the list of tasks. You need to do a count($this->dashboard_model->getTasks()) or i guess there is a count method on tasks collection. Commented Nov 21, 2018 at 12:05
  • @JagjeetSingh when I echo $total['totaltasks'] I get undefined index totaltasks but when I var_dump($total)` I get array(1) { [0]=> object(stdClass)#22 (1) { ["totaltasks"]=> string(1) "4" } } Commented Nov 21, 2018 at 12:07
  • @ThomasLefetz getTasks() return the number of tasks as a number Commented Nov 21, 2018 at 12:08
  • @user1 use count count($total) Commented Nov 21, 2018 at 12:08

2 Answers 2

1

result() This method returns the query result as an array of objects, or an empty array on failure update getTasks like below

public function getTasks(){ $this->db->Select("COUNT(taskID) as totaltasks"); $this->db->from('tasks'); $query = $this->db->get(); return $query->result()[0]->totaltasks; } 
Sign up to request clarification or add additional context in comments.

Comments

0

You can use row() to get single row instead of result() when you already know that you will get one row as a result

 public function getTasks(){ $this->db->Select("COUNT(taskID) as totaltasks"); $this->db->from('tasks'); $query = $this->db->get(); return $query->row()->totaltasks; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.