2012-10-19 33 views
1

我有2个问题通过另一个php文件定义值编辑

1.)如何编写update_defile($array_value){...}函数?

define_file.php

<?php 
    define("FIST_NAME", "something1"); 
    define("LAST_NAME", "something2"); 
    define("ADDRESS", "something3"); 
?> 

“东西” 是不是一个恒定值,它可以改变每一个方法Call(update_defile($array_value)

值设置

$array_value = ("FIST_NAMe" => "duleep", "LAST_NAME" => "dissnayaka", "AGE" => "28"); 

后调用方法(update_defile($ array_value ){.....})“define_file.php” 文件想要看起来像波纹管

)。
<?php 
    define("FIST_NAME", "duleep"); 
    define("LAST_NAME", "dissnayaka"); 
    define("ADDRESS", "something3"); 
    define("AGE", "28"); 
?> 

2)。

我的数据库是Oracle。我已经将配置值保存在数据库中,但经常将这些配置值用于我的应用程序。所以我得到价值形式数据库并保存在define_file.php作为增加性能(降低率数据库调用),但我不知道我可以提高性能保持PHP文件中的配置值请解释。什么是提高我的应用程序和其他替代解决方案的性能的最佳方式。

回答

1
 public function update($form_config_arr) 
    { 

     if((is_readable($config_file_path)) && is_writable($config_file_path)) 
     { 
     if(!$config_old_file_content = file_get_contents($config_file_path)) 
     { 
      throw new Exception('Unable to open file!'); 
     } 

     $i = 0; 
     $config_old_arr = array(); 
     $config_new_arr = array(); 

     foreach ($form_config_arr as $constant => $value){ 
      $config_old_line = $this->getLine($constant); 
      $config_old_arr[$i] = $config_old_line; 

      if(($value == 'true') || ($value == 'false')){ 
       $config_new_arr[$i] = "define ('$constant', $value);\n"; 
      }else{ 
       $config_new_arr[$i] = "define ('$constant', '$value');\n"; 
      } 
      $i++; 
     } 

     $config_new_file_content = str_replace($config_old_arr, $config_new_arr, $config_old_file_content); 
     $new_content_file_write = file_put_contents($config_file_path, $config_new_file_content); 

     foreach ($config_new_arr as $constant => $value) 
     { 
      echo $value.'<br/>'; 
     } 
     return true; 
     }else{ 
     throw new Exception('Access denied for '.$config_file_path); 
     return false; 
     } 

    } 

    /** 
    * 
    * @param string $constant 
    * @return string 
    */ 
    private function getLine($constant) 
    { 
     $match_line = ''; 
     $config_file = fopen($config_file_path, "r"); 
     if($config_file) 
     { 
     //Output a line of the file until the end is reached 
     $i = 0; 
     while(!feof($config_file)) 
     { 
      $i++; 
      $config_old_line = fgets($config_file); 
      $pos = strpos($config_old_line, $constant); 
      if($pos !== false) 
      { 
       $match_line= $config_old_line; 
      } 
     } 
     fclose($config_file); 
     return $match_line; 
     }else{ 
     throw new Exception('Unable to open file!'); 
     } 

    } 
+0

欢迎提高我的代码:) – Duleep

0

你正在做的是编辑一个文件。 只需创建另一个PHP脚本:updater.php 应该查询数据库,获取价值观和define_file.php更新值

查找PHP文件处理功能在这里:http://www.w3schools.com/php/php_file.asp

+0

但我的客户需要一个位置,以保持对于编辑define_file.php文件 – Duleep

1

为什么不能u使用会话要存储这些值,那么你可以从脚本中的任何地方访问和修改 。

<?php 
    session_start(); 
    $_SESSION["FIST_NAME"]= "something1"; 
    $_SESSION["LAST_NAME"]= "something2"; 
    $_SESSION["ADDRESS"]= "something3"; 


?> 
+0

这是好主意,但define_file.php文件主要原因有超过200个变量的配置值。所以我觉得会议不能处理那种记忆。任何方式感谢您的反馈 – Duleep

+0

实际上定义是用于常量,那么你不应该改变它在运行时设置一次。 –

+0

确定我的主要想法是为每个Web应用程序创建配置文件。每个客户都有单独的配置文件 – Duleep

相关问题