Colline
Colline

Reputation: 99

Create another controller and model not working on codeigniter

I just want to ask if how can I add another controller and model on codeigniter. So far I have 1 controller and 1 model for now and they are working now I tried adding 1 more controller and 1 more model like this

controller

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Investor extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/index.php/welcome
     *  - or -
     *      http://example.com/index.php/welcome/index
     *  - or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see https://codeigniter.com/user_guide/general/urls.html
     */
     public function __construct()
    {
        header("Access-Control-Allow-Origin: *");
        parent::__construct();
        $this->load->model('investor_m');
        $this->load->helper('url');
        $this->load->library("pagination");
        $this->load->library("session");
    }

    public function index()
    {
        $data['title'] = 'Lending System Login';
        $data["count_investor"] = $this->investor_m->get_all_investor();
        $this->template->load('default_layout','contents','investors', $data);
    }
}

model

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class investor_m extends CI_Model {

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    public function get_all_investor()
    {
        return $this->db->count_all("investor");
    }
}

and on my view

<?php foreach ($count_investor as $rec){echo $rec;} ?>

Could someone help me out why it's not working . The error says that

A PHP Error was encountered Severity: Notice

Message: Undefined variable: count_investor

Filename: views/investors.php

Line Number: 12

could someone help me out.

Upvotes: 1

Views: 51

Answers (1)

Rajdip Chauhan
Rajdip Chauhan

Reputation: 345

You mention wrong class name in both model and controller, you create model name with employee_m and you try to extend it with name investor. It should be like below

Model

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class investor extends CI_Model {

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    public function get_all_investor()
    {
        return $this->db->count_all("investor");
    }
}

Controller

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Investor extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/index.php/welcome
     *  - or -
     *      http://example.com/index.php/welcome/index
     *  - or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see https://codeigniter.com/user_guide/general/urls.html
     */
     public function __construct()
    {
        header("Access-Control-Allow-Origin: *");
        parent::__construct();
        $this->load->model('investor');
        $this->load->helper('url');
        $this->load->library("pagination");
        $this->load->library("session");
    }

    public function index()
    {
        $data['title'] = 'Lending System Login';
        $data["count_investor"] = $this->investor->get_all_investor();
        $this->template->load('default_layout','contents','investors', $data);
    }
}

Hope this will help you.

Upvotes: 1

Related Questions