Twey, it gives a Parse error: parse error, unexpected $ in /var/www/html/shachi/php/test/testup.php on line 38
Printable View
Twey, it gives a Parse error: parse error, unexpected $ in /var/www/html/shachi/php/test/testup.php on line 38
Twey: Are you around??
Any help here??
Any Help Here???
Whoops, typo. Try this one:Code:<?php
define('DATABASE_FILE', 'database_fake.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([url=http://www.php.net/file]file[/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, $lines[$i]);
$changeThisLine = false;
foreach($conditions as $k => $v)
if($changeThisLine = ($cline[$k] == $v)) continue;
foreach($changes as $k => $v)
$cline[$k] = $v;
++$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');
?>
Twey, It didn't work.:(
But I tried to update the code from liro from the other post and came up with something like this:
(For test purposes used online.txt with | as separator the original database will have ,)
But it has a problem that if the values are not supplied it replaces the second and third values with the second and third values of the first line. I need a function to loop through every line of the array searching for a key and then give the index number of the array. So far I have made something like this:Code:<?php
function replaceLine($f, $etsi, $first, $second) {
if(!file_exists($f)) {
die("Sorry the file " . $f . " could not be found.");
} else {
$filearr = file($f);
$sisalto;
for($i = 0; $i < count($filearr); $i++) {
list($id, $nimi, $passu) = explode("|", $filearr[$i]);
if($first == ""){
$first = $nimi;
}
if($second == ""){
$second = $passu;
}
if($id == $etsi) {
$sisalto .= $id . "|" . $first . "|" . $second . "\n";
} else {
$sisalto .= $filearr[$i];
}
}
$fi = fopen($f, "w");
fwrite($fi, $sisalto);
fclose($fi);
}
}
replaceLine("online.txt", "Test7", "", "");
?>
online.txt looks something like this:Code:<?php
$loopIndex = 0;
$test = array('Test0','Test1','Test2','Test3');
#$test = file("online.txt");
while($test[$loopIndex] != "Test4|test5|test6" && $loopIndex < 4){
$loopIndex++;
}
if($loopIndex < 4){
echo "Found supplied value at index ".$loopIndex;
}
#echo $test[3];
?>
But as soon as I uncomment the $test = file("online.txt"); it shows nothing.Code:Test1|test2|test3
Test4|test5|test6
Test7|test8|test9
Test10|test11|test12
I have tried my best but could not come up with a suitable code. I have almost lost.
Any ideas??
You ought to know by now not to say "it didn't work." :) What went wrong?Quote:
Twey, It didn't work.
Oops!! Sorry. It doesn't update the file. I have been trying to fix it but unforutnately I don't have much brain about PHP. :(
This is tested:It works, but not with the line of PHP code in the file. You should remove it.Code:<?php
define('DATABASE_FILE', 'database_fake.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, $lines[$i]);
$changeThisLine = false;
foreach($conditions as $k => $v)
if(!($changeThisLine = ($cline[$k] == $v))) break;
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');
?>
Twey, it edits all the user's information. For example if the database_fake.php is like this:
Then after executing the PHP script it changes the values to :Code:Test,test,test@test.com,sweden
testuser,test,test@user.com,uk
And by the way if I remove the <?php exit()?> lines then anyone would be able to view the database(sure I could encrypt the password with md5 hash but what about the email addresses??They'd be a great source for spambots).Code:Test,test,some.new@email-address.com,NePaL
testuser,test,some.new@email-address.com,NePaL
I am sorry to make you write bunch of codes and take up your time but I need help and I cannot do it myself. You are more experienced than me.