【发布时间】:2019-10-03 09:56:41
【问题描述】:
我只是想问一下如何在 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
谁能帮帮我。
【问题讨论】:
-
你可以试试 var_dump($this->investor->get_all_investor());死;在 index() 函数的第一行,看看数组有什么?
-
它没有显示任何先生
-
如果
var_dump($this->investor->get_all_investor());什么也没有显示,那是你的问题。这意味着在模型中调用$this->db->count_all("investor");由于某种原因而失败。 “投资者”是表的名称吗?如果有,有记录吗?你知道db->count_all返回一个代表表中行数的整数吗?您是否开启了错误报告?
标签: php codeigniter