user1
user1

Reputation: 262

can't pass data to view

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?

Upvotes: 0

Views: 51

Answers (2)

Mithu CN
Mithu CN

Reputation: 595

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

Upvotes: 0

suresh bambhaniya
suresh bambhaniya

Reputation: 1687

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

Upvotes: 1

Related Questions