php - 创建另一个控制器和模型不适用于 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
有人可以帮帮我吗?
解决方案
您在模型和控制器中都提到了错误的类名,您使用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);
}
}
希望这会帮助你。
推荐阅读
- r - 延迟 downloadHandler 直到生成图
- javascript - 为什么我在 React/Node/Express 应用程序中单击注销按钮两次以清除会话 cookie?
- python - 在 scipy.integrate.solve_ivp python 中传递矩阵作为输入
- c# - VS 2019 - ASP.NET Core MVC Web 应用程序(用 C# 编写):全新项目上的多个错误
- flutter - Flutter - 保留不可见的gridview子项
- visual-studio - 当我在字符串中按 Enter 时,Visual Studio 会自动放置加号。我怎样才能禁用它?
- time-complexity - 求 n0 和常数因子 c 的值,使得 f(n) = n log n 为 Ω(n)
- go - go mod 和 go get 的好习惯
- arrays - 当我尝试初始化 100 个元素的一维数组并使用指针填充它时,我遇到了分段错误(核心转储)
- routes - Vaadin (Flow) 21 从 20 升级 - 所有路线突然返回 404