Search code examples
phpfilefwritefread

How can I check if a username already exists in a file and also add his points to him?


I intentionally want to use a text file to do this. So I read a text file and I want to check if a username already exists in that text file or not and I want to either add this username to the text file if he doesn't exists or just add the points to him.

My current code:

<?php
$myfile = fopen("test.txt", "r") or die("Unable to open file!");
$file = fread($myfile,filesize("test.txt"));
//echo $file;
fclose($myfile);

//$username = $_REQUEST['username'];
//$points = $_REQUEST['point'];
$username = 'chinmay'; //chinmay is a username this is unique
$points = 200; //if username chinmay not exitst then Insert first time otherwise if username chimay exist then next onwards this point will update everytime in the text file.

$myfileWrite = fopen("test.txt", "a") or die("Unable to open file!");
$txt = $username."|".$points."\n";
fwrite($myfileWrite, $txt);

fclose($myfileWrite);
?> 

test.txt:

chinmay|800
john|200
sanjib|480
debasish|541

This is my complete code. My requirement is:

  • \n is not working when I am using this text inserted in the same line.
  • How can I check duplicate username?
  • If I found username then how can I update user points?

I googled last 2 hours but not getting any solution. I have no idea about this problem.


Solution

  • This should work for you:

    First use file() to read your file into an array. Then you can use array_map() to loop through each line and explode() it by | as delimiter. After this you can use array_column() to get the username as key for the points as value. Like this:

    Array
    (
        [chinmay] => 1200
        [john] => 200
        [sanjib] => 480
        [debasish] => 541
        [chinmayx] => 200
    )
    

    With the array you can simply check if the username already exists or not. If not add it to the array and then add the points to it.

    After adding the points to the username you can change your data back in the same format and save it with file_put_contents().

    Full code:

    <?php
    
        $lines = file("test.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        $usernames = array_column(array_map(function($v){
            return explode("|", $v);
        }, $lines), 1, 0);
    
        $username = "chinmayx";
        $points = 200; 
    
        if(!isset($usernames[$username]))
            $usernames[$username] = 0;
        $usernames[$username] += $points;
    
        foreach($usernames as $k => $v)
            $data[] = "$k|$v" . PHP_EOL;
    
        file_put_contents("test.txt", $data);
    
    ?>
    

    EDIT:

    If you have PHP under 5.5 just replace:

    $usernames = array_column(array_map(function($v){
        return explode("|", $v);
    }, $lines), 1, 0);
    

    with this:

    $lines = array_map(function($v){
        return explode("|", $v);
    }, $lines);
    
    $usernames = array_combine(
        array_map(function($v){
            return $v[0];
        }, $lines),
        array_map(function($v){
            return $v[1];
        }, $lines)
    );
    

    Also if you want to get the TOP 10 users, just rsort() your array and then take an array_slice() of the first 10 elements, e.g.

    rsort($usernames);
    $topUsers = array_slice($usernames, 0, 10);
    print_r($topUsers);