1

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.

3
  • can you try to var_dump($this->investor->get_all_investor()); die; on the first line of index() function and see what the array has ? Commented Oct 3, 2019 at 10:07
  • it doesn't display any sir Commented Oct 3, 2019 at 13:03
  • If var_dump($this->investor->get_all_investor()); shows nothing then that is your problem. That means that in the model the call $this->db->count_all("investor"); Is failing for some reason. Is "investor" the name of the table? If it is, does it have any records? Do you understand that db->count_all returns an integer which represents the number of rows in the table? Do you have error reporting turned on? Commented Oct 3, 2019 at 20:10

1 Answer 1

1

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.

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

3 Comments

still didn't work sir . Do I need to setup any on the router?
You do not need to setup anything on the router.
$this->load->view('default_layout','contents','investors', $data), think this will work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.