0

I have a foreach loop that should change the value of one of the settings in the "settings" array. However this does not stick outside of the foreach loop. The entire function:

    public function GenerationModifiers(){
    $query = "SELECT `modifiers` FROM `settings`";
    $data = mysqli_query($this->dbc, $query);
    $row = mysqli_fetch_array($data);
    $modifiers = $row['modifiers'];
    $modifiers = explode(";", $modifiers);
    foreach($modifiers as $modifier){
        $mod = explode(".", $modifier);
        $control = $mod[0];
        $setting = $mod[1];
        switch($control){
            case "moduleOff":
                $modules[$setting]['enabled'] = 0;
                print_r($modules[$setting]);
                break;
            case "settingsChange":
                $s = explode(":", $setting);
                $toChange = $s[0];
                $changeTo = $s[1];
                $this->settings[$toChange] = $changeTo;
                print_r($this->settings[$toChange]);
                break;
        }
    }
}

This function is located in a class which also has a very similar constructor, that also sets $this->settings; Where it's used in page generation:

class pageGeneration {
    public function __construct($settings, $version, $dbc, $layout, $core, $parser, $admin){
        $this->settings = $settings;
        $this->version = $version;
        $this->dbc = $dbc;
        $this->layout = $layout;
        $this->core = $core;
        $this->parser = $parser;
        $this->admin = $admin;

    }
    public function Generate(){
        $this->core->GenerationModifiers(); ... //More unneeded code here

I've seen people do it like this;

foreach($modifiers as &$modifier){

But that won't work here as I'm changing the "settings" array, right?

17
  • Do your values print when you call print_r? If not your $control variable probably doesn't have the values you're expecting. Commented Aug 25, 2015 at 14:07
  • @Cfreak Yes, that's how I know that they change within the foreach but not outside of it. Commented Aug 25, 2015 at 14:09
  • what does the data look like before you start calling explode? (the value of $modifier) Commented Aug 25, 2015 at 14:13
  • @Cfreak It looks like: moduleOff.forums;settingsChange.home_display:test Commented Aug 25, 2015 at 14:15
  • 1
    I think this is scope issue. Try $this->core->GenerationModifiers($this->settings); and change the called function to use public function GenerationModifiers(&$settings){ etc Commented Aug 25, 2015 at 14:34

1 Answer 1

3

Just to be sure we are singing off the same song sheet I have put what I said in an anwer. If it turns out to be fooy I will delete this.

public function GenerationModifiers(&$settingsIN){      //<-- changed

    echo 'In GenerationModifiers before trying to change';
    print_r($settingsIN);

    $query = "SELECT `modifiers` FROM `settings`";
    $data = mysqli_query($this->dbc, $query);
    $row = mysqli_fetch_array($data);
    $modifiers = $row['modifiers'];
    $modifiers = explode(";", $modifiers);
    foreach($modifiers as $modifier){
        $mod = explode(".", $modifier);
        $control = $mod[0];
        $setting = $mod[1];
        switch($control){
            case "moduleOff":
                $modules[$setting]['enabled'] = 0;
                print_r($modules[$setting]);
                break;
            case "settingsChange":
                $s = explode(":", $setting);
                $toChange = $s[0];
                $changeTo = $s[1];
                $settingsIN[$toChange] = $changeTo;      //<-- changed
                print_r($settingsIN[$toChange]);         //<-- changed
                break;
        }
    }
}

Called from

class pageGeneration {
    public function __construct($settings, $version, $dbc, $layout, $core, $parser, $admin){
        $this->settings = $settings;
        $this->version = $version;
        $this->dbc = $dbc;
        $this->layout = $layout;
        $this->core = $core;
        $this->parser = $parser;
        $this->admin = $admin;

    }
    public function Generate(){
        print_r($this->settings);  // loop before you leap

        $this->core->GenerationModifiers($this->settings); 

        echo 'settings on return from core method call';
        print_r($this->settings);  // loop after we leapt
Sign up to request clarification or add additional context in comments.

4 Comments

Ah now we have 2 $settings Will look at that!
Yes, this did work while having the function in the Core class. Now, to change the modules setting the correct way
Ok try now, I called the method Param $settingIN its ugly but stops confusion between the exploded value you called $settings
This worked perfectly! Thank you for all of your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.