使用codeigniter在Helper文件中调用模型

 beng83790si 发布于 2023-02-12 14:29

我想编写一个函数来加载辅助文件中的下拉列表,因此我想在Helper文件中使用我的模型.

当我使用它时,它给我错误:

$this->load->model("news_model");

错误:

Fatal error: Using $this when not in object context in C:\xampp\test\application\helpers\component_helper.php on line 6

我的方法:

function dropdown($Class,$Attribute)
{
$Output=NULL;
$ClassName=$Class."_model";
$this->load->model($ClassName);
$FullData=$ClassName->get();
foreach ($FullData as $Data) 
{
    $Output.='';
}
return $Output;
}

谢谢

1 个回答
  • 查看这篇文章:

    function my_helper()
    {
        // Get a reference to the controller object
        //$CI = get_instance();
        // use this below
        $CI = &get_instance();
    
        // You may need to load the model if it hasn't been pre-loaded
        $CI->load->model('my_model');
    
        // Call a function of the model
        $CI->my_model->do_something();
    }
    

    /sf/ask/17360801/

    2023-02-12 14:30 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有