02.19 php saved
dardosordi
Tags add more
store config file  
Note
stores data in a config file
  1. function _storeConfig($name, $data = array(), $read = false)
  2. {
  3.     $content = '';
  4.     foreach ($data as $key => $value) {
  5.         $content .= "\$config['$name']['$key']";
  6.  
  7.         if (is_array($value)) {
  8.             $content .= " = array(";
  9.             foreach ($value as $key1 => $value2) {
  10.                 $value2 = $this->__formatArray($value2);
  11.                 $content .= "'$key1' => $value2, ";
  12.             }
  13.             $content .= "\n);\n";
  14.         } else {
  15.             if (is_bool($value)) {
  16.                 $value = $value?'true':'false';
  17.             } elseif (is_string($value)) {
  18.                 $value = "'".addslashes($value)."'";
  19.             }
  20.             $content .= " = $value;\n";
  21.         }
  22.     }
  23.  
  24.     $content = "<?php\n".$content."?>";
  25.  
  26.     uses('File');
  27.     $name = strtolower($name);
  28.     $file =& new File(CONFIGS.$name.'.php');
  29.     if ($file->open('w')) {
  30.         $file->append($content);
  31.     }
  32.     $file->close();
  33.     if ($read) {
  34.         Configure::load($name);
  35.     }
  36. }
  37.  
  38. function __formatArray($data = array(), $level = 1) {
  39.     if (!is_array($data)) {
  40.         if (is_bool($data)) {
  41.             return $data?'true':'false';
  42.         }
  43.         if (is_string($data)) {
  44.             $data = "'".addslashes($data)."'";
  45.         }
  46.         return $data;
  47.     }
  48.     $content = " array(";
  49.     $tab = str_repeat("\t", $level);
  50.     foreach ($data as $key => $value) {
  51.         $value = $this->__formatArray($value, $level + 1);
  52.         $content .= "\n$tab'$key' => $value,";
  53.     }
  54.     $content .= "\n$tab)";
  55.     return $content;
  56. }
  57.  
Parsed in 0.099 seconds, using GeSHi 1.0.7.14

Modify this Paste