0

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);
 }
Travis
  • 657
  • 6
  • 24
  • So have you made any attempt to achieve your result? Have you have, you should include that attempt along with the result and how it differs from what you want. If you _haven't_, you should do so now (well, ideally, _before_ you post your question) – Patrick Q Jun 07 '19 at 12:41
  • @PatrickQ I've tried a few ways to append, but I just get page errors, I'll edit in. – Travis Jun 07 '19 at 12:42
  • As a side note, if your "config" is going to be edited regularly, why aren't you using a database table instead of a text file? – Patrick Q Jun 07 '19 at 12:42
  • This is the way it was set up by my mentor, going to be doing this for permissions for certain things. – Travis Jun 07 '19 at 12:49
  • 1
    In your second attempt you have `$config_data[$section][$key] = $old_val;`. I don't see `$old_val` defined anywhere. How are you expecting that to have a value? – Patrick Q Jun 07 '19 at 13:06

1 Answers1

1

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);
}
tcj
  • 1,645
  • 4
  • 13
  • 21
  • You're welcome, if you want to add a space between each values for better readability you can use `$config_data[$section][$key] .= ', ' . $value;` in the `else` statement. – tcj Jun 07 '19 at 13:24