Aha, seems we had a few extraneous linebreaks hanging around:
Code:
<?php
define('DATABASE_FILE', 'database.php');
define('SEPARATOR', ',');
define('LINEBREAK', "\n");
define('USERNAME', 0);
define('PASSWORD', 1);
define('EMAIL', 2);
define('COUNTRY', 3);
define('ZIP', 4);
function updateUser() {
$ret = 0;
$lines = array_unique(file(DATABASE_FILE));
$argv = func_get_args();
$conditions = array();
$changes = array();
for($i = 0; $i < count($argv) && $argv[$i] !== SEPARATOR; $i += 2)
$conditions[$argv[$i]] = $argv[$i + 1];
for(++$i; $i < count($argv); $i += 2)
$changes[$argv[$i]] = $argv[$i + 1];
for($i = 0; $i < count($lines); ++$i) {
$cline = explode(SEPARATOR, str_replace(LINEBREAK, '', $lines[$i]));
if(count($cline) < 2) continue;
$changeThisLine = true;
foreach($conditions as $k => $v)
if($cline[$k] != $v)
$changeThisLine = false;
if(!$changeThisLine) continue;
foreach($changes as $k => $v)
$cline[$k] = $v;
$lines[$i] = implode(SEPARATOR, $cline);
++$ret;
}
$f = fopen(DATABASE_FILE, 'w');
fwrite($f, str_replace(LINEBREAK . LINEBREAK, LINEBREAK, implode("\n", $lines)));
fclose($f);
return $ret;
}
updateUser(USERNAME, 'Shachi', COUNTRY, 'nepal', SEPARATOR, COUNTRY, 'Nepal', EMAIL, 'some.new@email-address.com');
?>
The syntax is, for example:
Code:
updateUser(condition_column_1, condition_value_1, condition_column_2, condition_value_2, SEPARATOR, column_1, new_value_1, column_2, new_value_2)
... which would be similar to the SQL:
Code:
UPDATE table SET column_1=new_value_1, column_2=new_value_2 WHERE condition_column_1=condition_value_1 AND condition_column_2=condition_value_2;
Bookmarks