Jump to content

Undefined offset when using a flat file with pipe symbols


Eejut

Recommended Posts

Hi, I've got a flat file/text file which I'm currently using as a member database for my site (not advisable I know.. but still learning here).

 

In a section of code I need to separate the fields to make various checks later in the code

 

I use this line to do that..

 

list ($name, $pass, $fileidnumber) = split ('\|', $line);

 

Yet.. I keep getting "undefined offset" messages.. even when all 3 fields are filled in for all records.. eg..

Fred|bonkers|3\n

 

Is it the new line causing the problem?

 

Also, the server is a bit peculiar in behaviour, it won't let me upload empty text files to start with (0kb).. the only way around it is for me to add a blank line or something to make it at least 1kb so the server notices it.. so in other words.. if Fred's record was the only one in the database, there'd be a space (I guess) underneath his record (all future records are added to the top of the file, but the space will always come last when checking)

 

If anyone out there can help or give ideas as to why this is happening I'd be grateful.. thanks

Link to comment
Share on other sites

Thanks for the help.. I've tried explode but it still keeps nagging about this "undefined offset" palarva

 

The text file basically looks like this..

 

Rod|hello|1

Jane|noodles|2

Freddie|walrus|3

 

The first field is username, 2nd field is password, 3rd field is id number

 

I'm using this line now instead (thanks for letting me know about "split" depreciation btw)..

 

list ($name, $pass, $id) = explode ('|', $line);

 

The silly thing is still complaining about undefined offsets (1 and 2 in this case)

Link to comment
Share on other sites

Sure.. here are the relevant bits..

 

$chatname=$_GET["chatname"];

//get viewer's id number

$idnumber;

$getid = fopen('nunyabiz.txt','r');

if (!$getid) {echo 'ERROR1: Unable to open file.<br>'; exit;}

while (!feof($getid))

  {

    $line = fgets($getid, 1024); //use 2048 if very long lines

    if ($line != "" || $line != "\n")

      {

        list ($name, $pass, $fileidnumber) = explode ('|', $line);

        if ($chatname == $name)

          {

            $idnumber=$fileidnumber;

          }

      } 

    $getid++;

  }

fclose($getid);

 

 

Link to comment
Share on other sites

This does nothing.

//get viewer's id number

$idnumber;

 

Stop with the fopen stuff. use something like this:

$lines = file('nunyabiz.txt');

// Loop through our array, show HTML source as HTML source; and line numbers too.

?why are you doing line numbers, you dont use them anywhere?

foreach ($lines as $line) {
list ($name, $pass, $fileidnumber) = explode ("|", $line);
$fileidnumber=trim($fileidnumber);//trim off the \n
        if ($chatname == $name)
          {
            $idnumber=$fileidnumber;
          }
}

Link to comment
Share on other sites

Thanks for cleaning up the code and that works fine now

 

I made the same changes for another loop checking a different file and for a while was still getting the same error but this time it was because there were 2 new lines at the end of that file!.. I'm going to learn mysql once this project is finished

 

Ta

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.