MxGucci Posted November 10, 2008 Share Posted November 10, 2008 Hi, I understand a lot of you would recommend I use mysql, however I have limited mysql databases, and all I need is to retrieve some information from a flat file which will contain about 10-20 lines of data. I have found a tutorial on the net that explained how to read from a flat file and echo it onto the page, however; now my problem is how do I specifically choose the page to show only from a line of my choice from the database. Here is the code I have used (get.php): <?php $fp = fopen('flat-file-data.txt','r'); if (!$fp) {echo 'ERROR: Unable to open file.</table></body></html>'; exit;} while (!feof($fp)) { $line = fgets($fp, 1024); //use 2048 if very long lines list ($field1, $field2, $field3, $field4, $field5, $field6) = split ('\|', $line); echo ' <table border="0"> <tr> <td>'.$field1.'</td> <td>'.$field2.'</td> <td>'.$field3.'</td> <td>'.$field4.'</td> <td>'.$field5.'</td> <td>'.$field6.'</td> </tr></table>'; $fp++; } fclose($fp); ?> Here is the flat file (flat-file-data.txt): row 1 cell 1|row 1 cell 2|row 1 cell 3|row 1 cell 4|row 1 cell 5|row 1 cell 6 row 2 cell 1|row 2 cell 2|row 2 cell 3|row 2 cell 4|row 2 cell 5|row 2 cell 6 row 3 cell 1|row 3 cell 2|row 3 cell 3|row 3 cell 4|row 3 cell 5|row 3 cell 6 row 4 cell 1|row 4 cell 2|row 4 cell 3|row 4 cell 4|row 4 cell 5|row 4 cell 6 row 5 cell 1|row 5 cell 2|row 5 cell 3|row 5 cell 4|row 5 cell 5|row 5 cell 6 /// (test the page at www.tiltnation.com/beta/test/get.php) Basically, I want to be able to have get.php do some sort of query string which will display to my visitor the line of their choice, this script is being used to review some websites, so I want to be able to do get.php?page=1 and it'll show only the data from line 1 and so forth. I am sure this isn't as easy as I expect but if anyone can either help give me a tutorial they have read or written, or a small snippet that I can look at, or even help create a simple version of what I want, it would be much appreciated. Thanks, Gucci Link to comment https://forums.phpfreaks.com/topic/132194-solved-reading-flat-file-database-need-help-showing-per-line/ Share on other sites More sharing options...
premiso Posted November 10, 2008 Share Posted November 10, 2008 I would use www.php.net/file over fopen and fgets. File will put the file into an array without doing all that nasty looping. Then to pull the first line, simply access it by: <?php $fileArray = file('text.txt'); if (isset($fileArray[$_GET['page']])) { echo $fileArray[$_GET['page']]; }else { echo 'That line does not exist'; } ?> Link to comment https://forums.phpfreaks.com/topic/132194-solved-reading-flat-file-database-need-help-showing-per-line/#findComment-687074 Share on other sites More sharing options...
MxGucci Posted November 10, 2008 Author Share Posted November 10, 2008 I would use www.php.net/file over fopen and fgets. File will put the file into an array without doing all that nasty looping. Then to pull the first line, simply access it by: <?php $fileArray = file('text.txt'); if (isset($fileArray[$_GET['page']])) { echo $fileArray[$_GET['page']]; }else { echo 'That line does not exist'; } ?> So there is no option in getting my code that I've pasted above to work in the way I want it? Link to comment https://forums.phpfreaks.com/topic/132194-solved-reading-flat-file-database-need-help-showing-per-line/#findComment-687081 Share on other sites More sharing options...
premiso Posted November 10, 2008 Share Posted November 10, 2008 You were specifically asking if you could pull just one line out of the file....If you wanted to integrate it with your code I would do this: <?php $fileArray = file('text.txt'); if (isset($fileArray[$_GET['page']])) { list ($field1, $field2, $field3, $field4, $field5, $field6) = split ('\|', $fileArray[$_GET['page']]); echo ' <table border="0"> <tr> <td>'.$field1.'</td> <td>'.$field2.'</td> <td>'.$field3.'</td> <td>'.$field4.'</td> <td>'.$field5.'</td> <td>'.$field6.'</td> </tr></table>'; }else { echo 'That line does not exist'; } ?> That should hopefully produce the result you wanted on getting the exact line out....maybe I am mis-understanding you. Link to comment https://forums.phpfreaks.com/topic/132194-solved-reading-flat-file-database-need-help-showing-per-line/#findComment-687087 Share on other sites More sharing options...
MxGucci Posted November 10, 2008 Author Share Posted November 10, 2008 The code you gave me doesn't work, unless I am using it wrong, am I suppose to direct my page ..get.php?=X or something to retrieve the line I want? I am so sorry for being so confusing. Thanks for your help Link to comment https://forums.phpfreaks.com/topic/132194-solved-reading-flat-file-database-need-help-showing-per-line/#findComment-687101 Share on other sites More sharing options...
premiso Posted November 10, 2008 Share Posted November 10, 2008 Yea, it should be ..get.php?page=1 Should return the first line of the file, actually looking at my script a modification is necessary: <?php $fileArray = file('text.txt'); $page = $_GET['page'] - 1; // minus 1 to account for array index. if (isset($fileArray[$page])) { list ($field1, $field2, $field3, $field4, $field5, $field6) = split ('\|', $fileArray[$page]); echo ' <table border="0"> <tr> <td>'.$field1.'</td> <td>'.$field2.'</td> <td>'.$field3.'</td> <td>'.$field4.'</td> <td>'.$field5.'</td> <td>'.$field6.'</td> </tr></table>'; }else { echo 'That line does not exist'; } ?> That should work when called get.php?page=1 it should display the first line page=2 should display the second line and so on. Link to comment https://forums.phpfreaks.com/topic/132194-solved-reading-flat-file-database-need-help-showing-per-line/#findComment-687129 Share on other sites More sharing options...
MxGucci Posted November 10, 2008 Author Share Posted November 10, 2008 SIR, THANK YOU SO MUCH FOR YOUR HELP!! I have spent hours, and hours, and HOURS, trying to figure this out. Kudos to you pal! Link to comment https://forums.phpfreaks.com/topic/132194-solved-reading-flat-file-database-need-help-showing-per-line/#findComment-687151 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.