【问题标题】:Codeigniter controller classCodeigniter 控制器类
【发布时间】:2014-02-24 10:39:56
【问题描述】:

我按如下方式创建了codeigniter控制器类

class Poems extends CI_Controller{

function __construct() {
    parent::__construct();
    $this->load->model('poem');
}

然后在上面的类中创建方法poe如下

function poem($poemID){
 $data['poem']=  $this->poem->get_poem($poemID);
 $this->load->view('poem',$data);

}

/************ *******/ 对于上面我在模型get_poem()方法中创建的控制器类如下

function get_poem(){
    $this->db->select()->from('poems')->where(array('active'=>1,'poemID'=>$poemID))->order_by('date_added', 'desc');
    $query=  $this->db->get();
    return $query->first_row('array');
}

当我在浏览器中运行这些诗歌时,它运行良好...... 当我想在浏览器中将诗歌方法加载为诗歌/诗歌时,会出现以下错误。

error 1: Missing argument 1 for Poems::poem()
error 2:  Undefined variable: poemID

我该如何解决这个错误

【问题讨论】:

    标签: codeigniter class controller


    【解决方案1】:

    模型中的 get_poem() 缺少参数。

    在模型中,像在控制器中一样使用get_poem($poemID)

    原因是,您正在调用该函数:

    get_poem($poemID)
               ^ with an argument
    

    然而,当你真正得到模型中的函数时,你会使用这个:

    get_poem()
             ^ without an argument
    

    这意味着$poemID 不仅不是get_poem 函数中的有效变量,而且该函数由于缺少参数而无法运行。

    旁注:写完这篇文章后,poem 听起来或看起来不再像一个真实的词了。

    【讨论】:

    • 我将参数添加到模型部分 get_poem($poemID) 仍然没有修复错误
    • 是的,有两个错误:缺少 Poems::poem() 的参数 1 和未定义的变量:在 controllers/poems.php 中的poemID ...
    • 如果您在控制器中获取它,那是因为您需要指定一个诗歌 ID。在浏览器中转到您的诗歌控制器,然后输入 /poemID 其中poemID 是您数据库中诗歌的实际 ID。
    • 仍然没有工作我在浏览器中尝试作为诗歌/poemID,但仍然找不到 404 页面。这是因为我在类(控制器)诗歌中没有带有poemID的功能。
    • 试试poems/poem/poemID@user3346201
    猜你喜欢
    • 1970-01-01
    • 2012-01-10
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多