Hi this is my first post so please bare with me, im currently learning php and im currently stuck with the code I have below.

what it does
It reads the 'first full line' of the flat file database.

This is the code I have that works as I described above:

<?php
$myFile = "members.php";
$fh = fopen($myFile, 'r');
$theData = fgets($fh);
fclose($fh);
echo $theData;
?>

what i need it to do
I need it to read the 'last line' and only the 'first word' of the last line from the flat file database. I need it so the output is creativep

This is the syntax of the flat file:
creativep|65457487454564548754gh454|me@email.com|dave creativep

*username|password|email|full name

*the flat file writes backwards, so the newest line is the bottom line

Hope ive explained myself correctly, any help would be appreciated.

Kind regards,
Dave

    Simplest way might be to read the entire file into an array via [man]file/man and then get the last line.

    <?php
    $file = 'members.php';
    $lines = file($file);
    while(!empty($lines))
    {
       $lastLine = trim(array_pop($lines));
       if(!empty($lastLine))
       {
          break; // this allows us to ignore any empty line(s) at end of file
       }
    }
    $fields = explode('|', $lastLine;
    echo $fields[0];
    ?>
    

    Of course, any of these file-based approaches could become resource-intensive if the file starts to get really large. A much more scalable approach would be to use a database instead of a flat file.

      Hi....thanks for the quick reply, i do agree with you about using a database instead of a flat file,that will be my next project 🙂 ....its just im learning as I go along and this input is helpfull to me.....

      I more or less understand your approach to the code, just one problem...

      im gettin a Parse error: syntax error, unexpected ';'
      message come up when I input the code

      please could you tell me were abouts the conflict is with it

      I learn better seeing other peoples stuff at the mo

      kind regards,
      Dave

        Hi again

        I found the problem 🙂

        $fields = explode('|', $lastLine;

        it was missing the bracket ) after the $lastline

        Thanks again for your help, its sites and people like this that make things that little bit easier for people like myself who are learning.

        Kind regards,
        Dave

          Yeah, uh...I left that parenthesis out on purpose as a debugging test for you. :rolleyes:

          Anyway, you're welcome.

          PS: Don't forget to mark this thread "Resolved" (if it is) via the "Thread Tools" menu at the top of the thread.

            Write a Reply...