如何根据数据库值创建文件? [英] How to create a file based on Database Values?

查看:105
本文介绍了如何根据数据库值创建文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





我获取的值如下:

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

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

我有 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))?真假;
}

我使用这个函数:

  $ 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文件中设置它。

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

 数组(
parameter_type=&参数=>值


,应该有:

  class parameter_m extends CI_Model {

public function get b $ b {
$ 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=> ''

);


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

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

这篇关于如何根据数据库值创建文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆