Re: Newbie Help? Navigating single records in a flat file
| From: | Justin French | Date: | Wed, 07 May 2003 00:55:36 +0000 |
| Subject: | Re: Newbie Help? Navigating single records in a flat file | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-146612@lists.php.net to get a copy of this message | ||
Assuming your data is a csv or tab-delimited file, with one record per line,
something like:
3,bob,smith,bob@nowhere.com,45,male
7,jane,doe,jane@foo.com,23,female
To print all data out in a nice format:
<?
// UNTESTED CODE
$row = 1;
$handle = fopen ("test.csv","r");
while ($line = fgetcsv ($handle, 1000, ","))
{
echo "ID: {$line[0]}<br />\n";
echo "Name: {$line[1]} {$line[2]}<br />\n";
echo "Age: {$line[4]} Sex: {$line[5]}<br />\n";
echo "Email: {$line[3]}<br />\n";
echo "<br />\n";
}
fclose ($handle);
?>
To only print out people with the first name bob
<?
// UNTESTED CODE
$row = 1;
$handle = fopen ("test.csv","r");
while ($line = fgetcsv ($handle, 1000, ","))
{
if(eregi('Bob',$line[1])
{
echo "ID: {$line[0]}<br />\n";
echo "Name: {$line[1]} {$line[2]}<br />\n";
echo "Age: {$line[4]} Sex: {$line[5]}<br />\n";
echo "Email: {$line[3]}<br />\n";
echo "<br />\n";
}
}
fclose ($handle);
?>
You can work the rest out from there, depending on what you want to do.
But really, this is only a good idea for VERY small amounts of data, because
you're reading the entire CSV file in on each instance (request) of the
script.
BTW, this was lifted pretty much straight out of the manual's fgetcsv()
page.
Justin
on 07/05/03 12:18 PM, Charlie (charlie@rahinston.com) wrote:
> Hi
>
> I am stuck how to approach a problem ... I need to navigate through the
> individual record in a flatfile database, which is tricky. I am not
> using mysql or any other database because the files are too small to
> warrent a database.
>
> I know what functions (like file(), next(), current() etc) to use but am
> not sure how to use them, depsite looking at php.net. I haven't had any
> luck with find any tutorials online that will assist me.
>
> Each record has to be displayed on an individual page, with links to
> next and previous records.
>
> Coudl someone show me how to approach writing such a script.
>
> Any help would be appreciated. Thanks in advance.
>
> Charlie
>