dsxmwin86342 2016-03-17 05:49
浏览 106
已采纳

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

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.

  • 写回答

1条回答 默认 最新

  • dongtuliao6760 2016-03-17 08:22
    关注

    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"        => ''
        )
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c