Eejut Posted January 9, 2010 Share Posted January 9, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/187880-undefined-offset-when-using-a-flat-file-with-pipe-symbols/ Share on other sites More sharing options...
thewooleymammoth Posted January 9, 2010 Share Posted January 9, 2010 use fwrite to create your files, and im not sure but i think explode() is what your looking for. could you post what your files look like? Quote Link to comment https://forums.phpfreaks.com/topic/187880-undefined-offset-when-using-a-flat-file-with-pipe-symbols/#findComment-991959 Share on other sites More sharing options...
laffin Posted January 9, 2010 Share Posted January 9, 2010 uhm, why not use explode, split is deprecated list ($name, $pass, $fileidnumber) = explode('|', $line); Quote Link to comment https://forums.phpfreaks.com/topic/187880-undefined-offset-when-using-a-flat-file-with-pipe-symbols/#findComment-991961 Share on other sites More sharing options...
thewooleymammoth Posted January 9, 2010 Share Posted January 9, 2010 ha i beat you too it. by like a second! Quote Link to comment https://forums.phpfreaks.com/topic/187880-undefined-offset-when-using-a-flat-file-with-pipe-symbols/#findComment-991963 Share on other sites More sharing options...
thewooleymammoth Posted January 9, 2010 Share Posted January 9, 2010 if for some reason you just want empty files, use touch() Quote Link to comment https://forums.phpfreaks.com/topic/187880-undefined-offset-when-using-a-flat-file-with-pipe-symbols/#findComment-991965 Share on other sites More sharing options...
Eejut Posted January 9, 2010 Author Share Posted January 9, 2010 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) Quote Link to comment https://forums.phpfreaks.com/topic/187880-undefined-offset-when-using-a-flat-file-with-pipe-symbols/#findComment-991987 Share on other sites More sharing options...
teamatomic Posted January 9, 2010 Share Posted January 9, 2010 Show us the file opening and the foreach. The list/explode is fine, something else is wrong. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/187880-undefined-offset-when-using-a-flat-file-with-pipe-symbols/#findComment-991992 Share on other sites More sharing options...
Eejut Posted January 9, 2010 Author Share Posted January 9, 2010 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); Quote Link to comment https://forums.phpfreaks.com/topic/187880-undefined-offset-when-using-a-flat-file-with-pipe-symbols/#findComment-991998 Share on other sites More sharing options...
thewooleymammoth Posted January 9, 2010 Share Posted January 9, 2010 you should post the entire actuall error also use code tags Quote Link to comment https://forums.phpfreaks.com/topic/187880-undefined-offset-when-using-a-flat-file-with-pipe-symbols/#findComment-992004 Share on other sites More sharing options...
teamatomic Posted January 9, 2010 Share Posted January 9, 2010 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/187880-undefined-offset-when-using-a-flat-file-with-pipe-symbols/#findComment-992020 Share on other sites More sharing options...
Eejut Posted January 10, 2010 Author Share Posted January 10, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/187880-undefined-offset-when-using-a-flat-file-with-pipe-symbols/#findComment-992226 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.