如何根据数据库值创建文件?

I want to create a .INI file based on the values I get from database. This are the values in my database for :

DB

Now in the field parameter filed is the value i want to write in file each in new line.

I fetch the values like this:

$this->data['params'] = $this->parameter_m->get();

So I get all the values from the table.

I want to write values in file like this:

[INIDetails]
SipUserName=
Password=
Domain=
Proxy=
Port=
SipAuthName=
DisplayName=
ServerMode=
UCServer=
UCUserName=
UCPassword=

[DialPlan]
DP_Exception=
DP_Rule1=
DP_Rule2=

[Advanced]
OperationMode=
MutePkey=
Codec=
PTime=
AudioMode=
SoftwareAEC=
EchoTailLength=
PlaybackBuffer=
CaptureBuffer=
JBPrefetchDelay=
JBMaxDelay=
SipToS=
RTPToS=
LogLevel=

I have WRITE Function that writes into file

function write($file = NULL, $file_content = array(), $sections = TRUE) {
    $this->file_content = (!empty($file_content)) ? $file_content : $this->file_content;
    $this->file = ($file) ? $file : $this->file;
    $this->sections = $sections;
    $content = NULL;

    if ($this->sections) {
        foreach ($this->file_content as $section => $file_content) {
            $content .= '[' . $section . ']' . PHP_EOL;
            foreach ($file_content as $key => $val) {
                if (is_array($val)) {
                    foreach ($val as $v) {
                        $content .= $key . '[]=' . (is_numeric($v) ? $v : $v ) . PHP_EOL;
                    }
                } elseif (empty($val)) {
                    $content .= $key . '=' . PHP_EOL;
                } else {
                    $content .= $key . '=' . (is_numeric($val) ? $val : $val ) . PHP_EOL;
                }
            }
            $content .= PHP_EOL;
        }
    } else {
        foreach ($this->file_content as $key => $val) {
            if (is_array($val)) {
                foreach ($val as $v) {
                    $content .= $key . '[] = ' . (is_numeric($v) ? $v : '"' . $v . '"') . PHP_EOL;
                }
            } elseif (empty($val)) {
                $content .= $key . ' = ' . PHP_EOL;
            } else {
                $content .= $key . ' = ' . (is_numeric($val) ? $val : '"' . $val . '"') . PHP_EOL;
            }
        }
    }

    return (($handle = fopen($this->file, 'w+')) && fwrite($handle, trim($content)) && fclose($handle)) ? TRUE : FALSE;
}

And i use that function like this :

$path = "./uploads/"; 
        $filename = "default.ini";
        $this->load->helper('file');
        $file = $path.$filename;
        $this->load->library('ini');
        $ini = new INI($file);
        $ini->write($file, $this->data['params']);

So i how to write the array of values i get from database into file ?

As you can see there is a filed called Parameter_Type i want to set it as section in INI file.

I'm guessing that your parameter_m is your model which have get() function where it returns array values from your table. What I think, the problem is that your model return incorrect structure of your array. Your array should have structure like:

array(
   "parameter_type" => array(
       "parameter" => value 
   )
)

in your model's get function, there should be something like:

class parameter_m extends CI_Model {

        public function get()
        {
                $query = $this->db->get('your_parameter_table');

                $assoc_arr = array();

                foreach ($query->result() as $row)
                {
                    $assoc_arr[$row->parameter_type][$row->parameter] = '';
                }

                return $assoc_arr;
        }

}

Using the get() it should output:

array(
    "INIDetails" => array(
        "SipUserName" => '',
        "Password"    => '',
        "Domain"      => '',
        "Proxy"       => '',
        "Port"        => '',
        "SipAuthName" => '',
        "DisplayName" => '',
        "ServerMode"  => '',
        "UCServer"    => '',
        "UCUserName"  => '',
        "UCPassword"  => ''
    ),
    "DialPlan"   => array(
        "DP_Exception" => '',
        "DP_Rule1"     => '',
        "DP_Rule2"     => ''
    ),
    "Advanced"   => array(
        "OperationMode"   => '',
        "MutePkey"        => '',
        "Codec"           => '',
        "PTime"           => '',
        "AudioMode"       => '',
        "SoftwareAEC"     => '',
        "EchoTailLength"  => '',
        "PlaybackBuffer"  => '',
        "CaptureBuffer"   => '',
        "JBPrefetchDelay" => '',
        "JBMaxDelay"      => '',
        "SipToS"          => '',
        "RTPToS"          => '',
        "LogLevel"        => ''
    )
);