Jump to content

[SOLVED] Read from Flat File (Query String Help)


MxGucci

Recommended Posts

Below is the code which I am using to read information from a flat file database, the script below functions properly (..index.php?site=1 .. brings up the first line in my flat file database and so forth)..

 


<?php
$fileArray = file('flat-file-data.txt');
$site = $_GET['site'] - 1;  // minus 1 to account for array index.
if (isset($fileArray[$site])) {
    list ($field1, $field2, $field3, $field4, $field5, $field6) = split ('\|', $fileArray[$site]);
    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';
}
?>

 

My question is, is there any possible way to amend the above script so I can retrieve information line by line but by directing my visitors to a text link such as (..index.php?site=pokerstars) instead of having (..index.php?site=1), could someone assist me in doing this?

 

I want to be able to define my own site name on each line, could we do this by storing a field that will contain the site name I want to use, and the script will some how read that line and direct my visitors to read from the line?

 

I am so sorry for making this question so confusing, I hope one of you helpful php guru's might help out a lost soul.

 

Thanks,

Gucci

Hi Gucci,

 

You can try adding a field, say at the beginning of each line for the site name and then scan each line for a match. Its pretty simple to do, see below:

 

<?php
$fileArray = file('flat-file-data.txt');
$sitename = $_GET['site'];

$found = false;
foreach ($fileArray as $line)
{
    list ($cur_sitename, $field1, $field2, $field3, $field4, $field5, $field6) = split ('\|', $line);
    if ($cur_sitename == $sitename) //Check the site name
    {
	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>';
	$found = true;
	break; //break out of the foreach 
    }
}
if (!$found)
echo 'That line does not exist';
?>

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.