Jump to content

[SOLVED] Reading Flat File Database - Need Help Showing Per Line


MxGucci

Recommended Posts

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

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';
}
?>

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?

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.