Reputation: 687
I have a ini file that I am supposed to add to with PHP Forms. As of right now I have working code to create a section if it does not exist, and replace a value if it exists. The thing I am struggling with is appending.
For Example this is my ini file right now.
[Persons]
names = "travis", "jackson"
color = "blue"
On the PHP side I have a form to insert section, key, and value
Say the input section is Person
, the key is color
and the value is red
.
The new ini file I want should be
[Persons]
names = "travis", "jackson"
color = "blue", "red"
The original code I am trying to modify: (taken from this stack article)
function config_set($config_file, $section, $key, $value) {
$config_data = parse_ini_file($config_file, true);
$config_data[$section][$key] = $value;
$new_content = '';
foreach ($config_data as $section => $section_content) {
$section_content = array_map(function($value, $key) {
return "$key=$value";
}, array_values($section_content), array_keys($section_content));
$section_content = implode("\n", $section_content);
$new_content .= "[$section]\n$section_content\n";
}
file_put_contents($config_file, $new_content);
}
My thoughts are just to append to current key, but I am not sure how to do that
EDIT: Attempts of things I have tried
This made zero change to the ini file before modification
function config_set($config_file, $section, $key, $value) {
$config_data = parse_ini_file($config_file, true);
$config_data[$section][$key] = $value;
$new_content = "[$section]\n$value";
foreach ($config_data as $section => $section_content) {
$section_content = array_map(function($value, $key) {
return "$key=$value";
}, array_values($section_content), array_keys($section_content));
$section_content = implode("\n", $section_content);
$new_content .= "\n$section_content\n";
}
file_put_contents($config_file, $new_content);
}
This broke the page
function config_set($config_file, $section, $key, $value) {
$config_data = parse_ini_file($config_file, true);
$config_data[$section][$key] = $old_val;
$new_content = '';
foreach ($config_data as $section => $section_content) {
$section_content = array_map(function($value, $key) {
return "$key=$old_val, $value";
}, array_values($section_content), array_keys($section_content));
$section_content = implode("\n", $section_content);
$new_content .= "[$section]\n$section_content\n";
}
file_put_contents($config_file, $new_content);
}
Upvotes: 0
Views: 836
Reputation: 1675
You could add this condition to check if there is a value inside "color" (key section) and append new value accordingly :
if (empty($config_data[$section][$key])) {
$config_data[$section][$key] = $value;
} else {
$config_data[$section][$key] .= ',' . $value;
}
Full code :
function config_set($config_file, $section, $key, $value) {
$config_data = parse_ini_file($config_file, true);
if (empty($config_data[$section][$key])) {
$config_data[$section][$key] = $value;
} else {
$config_data[$section][$key] .= ',' . $value;
}
$new_content = '';
foreach ($config_data as $section => $section_content) {
$section_content = array_map(function($value, $key) {
return "$key=$value";
}, array_values($section_content), array_keys($section_content));
$section_content = implode("\n", $section_content);
$new_content .= "[$section]\n$section_content\n";
}
file_put_contents($config_file, $new_content);
}
Upvotes: 1