【问题标题】:Create another controller and model not working on codeigniter创建另一个控制器和模型不适用于 codeigniter
【发布时间】: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-&gt;investor-&gt;get_all_investor()); 什么也没有显示,那是你的问题。这意味着在模型中调用 $this-&gt;db-&gt;count_all("investor"); 由于某种原因而失败。 “投资者”是表的名称吗?如果有,有记录吗?你知道db-&gt;count_all 返回一个代表表中行数的整数吗?您是否开启了错误报告?

标签: php codeigniter


【解决方案1】:

您在模型和控制器中都提到了错误的类名称,您使用 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);
    }
}

希望这会对你有所帮助。

【讨论】:

  • 仍然没有工作,先生。我需要在路由器上设置任何东西吗?
  • 你不需要在路由器上设置任何东西。
  • $this->load->view('default_layout','contents','investors', $data),认为这会起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-19
相关资源
最近更新 更多