Then, to modify the user with the username 'Twey' and set his country to "England":Code:// Defines.
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($ccol, $cval, $col, $val) {
$ret = 0;
$lines = file(DATABASE_FILE); // Read DATABASE_FILE into $lines as an array, broken at the linebreaks.
for($i = 0; $i < count($lines); ++$i) {
$cline = explode(SEPARATOR, $lines[$i]); // Read $lines[$i] into $cline as an array, broken at SEPARATOR.
if($cline[$ccol] == $cval) { // If this line matches the condition given,
$cline[$col] = $val; // set the specified column to the specified value.
$lines[$i] = implode(SEPARATOR, $cline); // Put $cline back together again, and insert it back into $lines.
++$ret;
}
}
$f = fopen(DATABASE_FILE, 'w'); // Open the file. Truncate to zero length (basically delete the file and create a new one; if memory corruption occurs here, bye-bye database)
fwrite($f, implode(LINE_BREAK, $lines)); // Put the lines back together again and write them back to the database
fclose($f);
return $ret; // Return the number of lines matched and modified
}
Sorry for the delay, I thought I'd posted, but evidently hadn't.Code:updateUser(USERNAME, 'Twey', COUNTRY, 'England');
