Welcome Guest, Not a member yet? Register   Sign In
loading helpers in a loop
#1

[eluser]xmonader[/eluser]
Hi, I'm trying to load helpers marked as "active_plugin" in the database settings

Code:
function load_activeplugins(){
        $activeps=$this->Settings_model->getActivePlugins();
        print_r($activeps);
        $helpers=array();
        foreach($activeps as $row){
            $pfullname=$row->setting_key;
            $pparts=explode("_", $pfullname);
            $pname=$pparts[0];
            $hname="myhelpers/$pname";
            $helpers[]=$hname;
        }
        print_r($helpers);
        $this->load->helper($helpers); // array supported by MY_Loader MS
    }

The result:
Quote:Array ( [0] => stdClass Object ( [setting_id] => 18 [setting_key] => censoring_helper [setting_value] => active_plugin ) [1] => stdClass Object ( [setting_id] => 19 [setting_key] => youtube_helper [setting_value] => active_plugin ) )
Array ( [0] => myhelpers/censoring [1] => myhelpers/youtube )

If I use
Code:
//$this->load->helper("myhelpers/youtube"); #THIS LINE WORKS
the youtube helper gets loaded. What's wrong ?
#2

[eluser]theprodigy[/eluser]
does it work if you modify it a bit and move the helper loading into the foreach loop?

Your code:
Code:
function load_activeplugins(){
        $activeps=$this->Settings_model->getActivePlugins();
        print_r($activeps);
        $helpers=array();
        foreach($activeps as $row){
            $pfullname=$row->setting_key;
            $pparts=explode("_", $pfullname);
            $pname=$pparts[0];
            $hname="myhelpers/$pname";
            $helpers[]=$hname;
        }
        print_r($helpers);
        $this->load->helper($helpers); // array supported by MY_Loader MS
    }
Suggested code:
Code:
function load_activeplugins(){
        $activeps=$this->Settings_model->getActivePlugins();
        print_r($activeps);
        $helpers=array();
        foreach($activeps as $row){
            $pfullname=$row->setting_key;
            $pparts=explode("_", $pfullname);
            $pname=$pparts[0];
            $hname="myhelpers/$pname";
            $this->load->helper($hname); // load them singly inside the loop
        }
    }
#3

[eluser]xmonader[/eluser]
Hi theprodigy, thanks for your help. I tried that first and didn't work Sad
#4

[eluser]theprodigy[/eluser]
Quote:$activeps=$this->Settings_model->getActivePlugins();

Does getActivePlugins() just return a call to get()?

If so, then you need to change your foreach loop to:
Code:
foreach($activeps->result() as $row){
// ...
}
//or in your case
foreach($activeps->result_array() as $row){
// ...
}
#5

[eluser]xmonader[/eluser]
Here's its declaration
Code:
function getActivePlugins(){
         $this->db->where(array('setting_value'=>'active_plugin'));
         $q=$this->db->get(self::TABLENAME);
         return $q->result();
     }
Note that was the output of print_r($activeps)
Quote:Array ( [0] => stdClass Object ( [setting_id] => 18 [setting_key] => censoring_helper [setting_value] => active_plugin ) [1] => stdClass Object ( [setting_id] => 19 [setting_key] => youtube_helper [setting_value] => active_plugin ) )
#6

[eluser]theprodigy[/eluser]
ok, quick question. You said that you tried to load each one individually inside the loop first, but it didn't work. Did it just not load anything, or did it produce an error?
#7

[eluser]xmonader[/eluser]
Yes, loading anyone individually works, and in the loop the first one only gets loaded!
No errors are produced it just quits :S
#8

[eluser]xmonader[/eluser]
Now it's getting very odd
Code:
function _load_activeplugins(){
            $activeps=$this->Settings_model->getActivePlugins();
            $ps=array("youtube", 'inflector');
            $this->load->helper($ps);
            /*
            foreach($activeps as $row){
                $pfullname=$row->setting_key;
                $pparts=explode("_", $pfullname);
                $pname=$pparts[0];
                $hname="myhelpers/$pname";
                echo "$hname";
                $this->load->helper($hname); // load them singly inside the loop
             }*/
    }
This loads "youtube", "inflector".
If I add anyother helpers i wrote it breaks down!
If I substitute "youtube" with "censoring" or "welcomepm" it works! just only one
#9

[eluser]xmonader[/eluser]
Fixed it, it seems the error was from duplicated names of functions
#10

[eluser]cahva[/eluser]
You should always use function_exists('function_name') in your helper functions to make sure it wont spit out an error on double initializing.
Code:
if (!function_exists('foo_bar'))
{
    function foo_bar($foo,$bar)
    {
        // Your code
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB