首页 > 解决方案 > 创建另一个控制器和模型不适用于 codeigniter

问题描述

我只是想问一下如何在codeigniter上添加另一个控制器和模型。到目前为止,我现在有 1 个控制器和 1 个模型,它们现在正在工作我尝试添加 1 个控制器和 1 个模型,像这样

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

在我的view

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

有人可以帮我弄清楚为什么它不起作用。错误说

遇到 PHP 错误 严重性:通知

消息:未定义变量:count_investor

文件名:views/investors.php

行号:12

有人可以帮帮我吗?

标签: phpcodeigniter

解决方案


您在模型和控制器中都提到了错误的类名,您使用employee_m创建模型名称并尝试使用名称Investor扩展它。它应该如下所示

模型

<?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");
    }
}

控制器

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

希望这会帮助你。


推荐阅读