Reputation: 335
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?
Upvotes: 0
Views: 93
Reputation: 94662
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
Upvotes: 3