MasOud MasOud - 5 months ago 45
PHP Question

Calling a model in a Helper file using codeigniter

I want to write a function for loading dropdown in helper file and for that reason I want to Use my models in Helper file.

When I use this it give me the error:

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


The Error:

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


my method:

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;
}


Thanks

Answer

Check this post:

function my_helper()
{
    // Get a reference to the controller object
    $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();
}

http://stackoverflow.com/a/2479485/1570901

Comments