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

Mas*_*our 0 php codeigniter

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

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

$this->load->model("news_model");
Run Code Online (Sandbox Code Playgroud)

错误:

Fatal error: Using $this when not in object context in C:\xampp\test\application\helpers\component_helper.php on line 6
Run Code Online (Sandbox Code Playgroud)

我的方法:

function dropdown($Class,$Attribute)
{
$Output=NULL;
$ClassName=$Class."_model";
$this->load->model($ClassName);
$FullData=$ClassName->get();
foreach ($FullData as $Data) 
{
    $Output.='<option value="'.$Data->Id.'">'.$Data->$Attribute.'</option>';
}
return $Output;
}
Run Code Online (Sandbox Code Playgroud)

谢谢

sra*_*vis 6

查看这篇文章:

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();
}
Run Code Online (Sandbox Code Playgroud)

/sf/answers/173563981/