Revision: 66479
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at May 13, 2014 03:30 by gitarfogas
Initial Code
// The input is a textfile: // key1=val1 // key2=val2 // key3=val3 // key4=val4 // . // . // . // The class: class UserID { private $UIDPath; private $UserUIDcontent; public function __construct($UserID, $Action, $UID_KEY, $UID_VAL, $Back) { $UID_Content = $this -> UIDPath = 'the/path/to/input/text.txt'; $UserUIDcontent = array(); if (file_exists($UID_Content) && (is_file($UID_Content))) { foreach (file($UID_Content) as $UIDlines) { list($key, $value) = explode('=', $UIDlines, 2) + array(NULL, NULL); if (trim($value) !== NULL) { $UserUIDcontent[$key] = trim($value); } } if ($Action == 'Read') { if ($UID_KEY == NULL) { $this -> UserUIDcontent = $UserUIDcontent; } else { if (array_key_exists($UID_KEY, $UserUIDcontent)) { $this -> UserUIDcontent = $UserUIDcontent[$UID_KEY]; } else { if ($UID_KEY == 'User') { return NULL; } else { unset($UserUIDcontent[$UID_KEY]); echo 'ERROR: The search key: ' . $UID_KEY . ' not available in Class: ' . __METHOD__; return; } } } } elseif ($Action == 'Write') { if (is_array($UID_KEY)) { if (count($UID_KEY) == count($UID_VAL)) { $UserUIDcontent = array_filter(array_merge($UserUIDcontent, array_combine($UID_KEY, $UID_VAL))); } else { echo 'ERROR: Keys: ' . count($UID_KEY) . ' and Values: ' . count($UID_VAL) . ' are not equal in Class: ' . __METHOD__; return; } } else { $UserUIDcontent[$UID_KEY] = $UID_VAL; } foreach ($UserUIDcontent as $key => $value) { if ($value !== NULL) { $UIDcontentTXT .= trim($key) . '=' . trim($value) . PHP_EOL; } } trim($UIDcontentTXT); $handle = fopen($UID_Content, "w"); fwrite($handle, $UIDcontentTXT, strlen($UIDcontentTXT)); fclose($handle); if ($Back != NULL) { if (array_key_exists($Back, $UserUIDcontent)) { return $this -> $UserUIDcontent = $UserUIDcontent[$Back]; } else { return $this -> $UserUIDcontent = $UserUIDcontent; } } else { return $this -> $UserUIDcontent = NULL; } } else { echo 'ERROR: The second parameter ' . $Action . ' not allowed in Class: ' . __METHOD__; return; } } else { echo 'ERROR: Required data source not found for Class: ' . __METHOD__; return; } } public function result() { return $this -> UserUIDcontent; } } // --------------------------------- // instances: // --------------------------------- $UserData = new UserID($UserID, 'Read', NULL, NULL, NULL); $array = $UserData -> result(); // Read full array $val = $UserData -> result()[key2]; // Read one data $UserData = new UserID($UserID, 'Read', 'key2', NULL, NULL); $val = $UserData -> result(); // Read one data $UserData = new UserID($UserID, 'Write', array('key2', 'aaaaaaaaa', 'User'), array(NULL, 'bbbbbbbb', NULL), NULL); $UserData; // write or delete more than one data by array $UserData = new UserID($UserID, 'Write', 'key2', NULL, NULL); $UserData; // write or delete one data // this is the problem: $UserData = new UserID($UserID, 'Write', array('User', 'key2', 'key3', 'key15'), array('aaaa', NULL, 'aaa', 'value15'), 1); $array = $UserData -> Array; // Write AND Read back full array or one key
Initial URL
Initial Description
I created this code for practice, and I looking for better and clear solution for the last instance where I can write or delete the data, but after I want to get back the content as array or the key as string. I want to use this write-read back with only one instance.
Initial Title
Read and write data to textfile
Initial Tags
class, array, text
Initial Language
PHP