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

时间:2016-03-17 05:49:50

标签: php codeigniter

我想根据从数据库获取的值创建 .INI 文件。 这是我的数据库中的值:

DB

现在,在字段中,参数字段是我想在新行中写入文件的值。

我获取这样的值:

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

所以我从表中得到了所有的值。

我想在文件中写入这样的值:

[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=

我有 WRITE 写入文件的功能

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;
}

我使用这样的功能:

$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']);

那么我如何将从数据库中获取的值数组写入文件?

如您所见,有一个名为 Parameter_Type 的字段,我想将其设置为INI文件中的部分。

1 个答案:

答案 0 :(得分:1)

我猜你的parameter_m是你的模型,它具有get()函数,它从表中返回数组值。我认为,问题是你的模型返回了不正确的数组结构。您的数组应具有以下结构:

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

在你的模型的获取功能中,应该有类似的东西:

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;
        }

}

使用get()输出:

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"        => ''
    )
);