Welcome Guest, Not a member yet? Register   Sign In
Problem working out how to make $CI->$variable work
#1

[eluser]J Maxwell[/eluser]
I have searched a bit for this, but strugging to work out how to achieve what I want.

I am querying a database, then dynamically loading assets based on the results of the query.

However, I need to do something like this:


Code:
foreach ($w as $widget) :

/* i've written an extension to the CI_Loader that enables me to add widgets.
* It loads them into $this->widget_name however, because i need to access them during a
* foreach loop, then I can't manually enter the name of the widget in order to use it, it needs
* to come from the variable
*/
$CI->load->widget($widget);
$CI->$widget->do_stuff_here();

endforeach;

The problem with this is it keeps throwing member function on non-object errors. How do I refer to the widget I just created without being able to type the name in?


Cheers,

John
#2

[eluser]WanWizard[/eluser]
Does your $this->load->widget() method assign the new class to the CI object?

Something simple as this would do it:
Code:
$CI =& get instance();
$CI->$widget = new $widget();
#3

[eluser]J Maxwell[/eluser]
Hey, thanks for that, I was sure there was a pretty simple explanation and I'd spent long enough staring blankly at the screen that I wasn't going to get it!

Problem fixed, many thanks.

John




Theme © iAndrew 2016 - Forum software by © MyBB