我想编写一个函数来加载辅助文件中的下拉列表,因此我想在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)
谢谢
查看这篇文章:
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)
| 归档时间: |
|
| 查看次数: |
4133 次 |
| 最近记录: |