I would like to read and write user-settings to a keyword-value file and be
able to extract it.  The following code works fine but i wondered if there
was a more elegant/direct way of doing this.

-Durwood

$file = "user_settings.txt";

// Read user settings from file if it exists, otherwise set to defaults and
// save them ... a separate method exists for users to change their
settings.
if ($is_readable($file)) {
  $parm = read_defaults($file);
} else {
  $parm['method'] = "bsort";
  $parm['tsize'] = 200;
  $parm['per_row'] = 5;
  $parm['sortby'] = "last_name";
  $parm['order'] = "ascending";
  write_defaults($file, $parm);
}

// Read keyword-value pairs from file into array
function read_defaults($default_file) {
  $fp = file($default_file);
  foreach ($fp as $line) {
     $line = trim($line);
     list ($key, $value) = explode(" ", $line);
     $param[$key] = $value;
  }
  return $param;
}

// Write keyword-value pairs from array
function write_defaults($default_file, $param) {
  $fp = fopen($default_file, "w");
  while (list($key, $value) = each ($param))
     fwrite ($fp, "$key $value\n");
  fclose($fp);
}



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to