Forum Moderators: coopster

Message Too Old, No Replies

Fun with txt and csv files!

Pulling data from a txt or csv file to poulate a form

         

TonyL

2:30 pm on Jan 17, 2004 (gmt 0)

10+ Year Member



help.

I have a database of about 20000 entries both in txt format and CSV format. I want to pull one line from the file based on an ID which is submitted from a previous form, so basically a user types in a code then clicks submit and the codes looks up this ID and displays just the info on that line in a HTML form?

Can this be done?

I want to avoid using a database where possible because of the sheer amount of data in it.

Cheers in advnace.

TonyL

justageek

2:37 pm on Jan 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are several ways to do it. But I'm not sure how you have your data astored. You say you don't want to use a database but you say you have the data in the database. Can you explain how you have your data a little more?

JAG

dmorison

2:44 pm on Jan 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



20000 records isn't a sheer amount of data (unless you've got massive amounts of data in each row) - it is nothing to any database you are likely to use in web based application. Even Microsoft Access can handle 20,000 rows in a table with no problem.

You will be far better off using a database, and taking advantage of proper indexing on your dataset.

To use a text file, Look at PHP's fgetcsv() [uk2.php.net] function. You don't have any easy option but to read the file until you find the row you want.

TonyL

2:57 pm on Jan 17, 2004 (gmt 0)

10+ Year Member



Ok. I agree the DB method would be much better (and a hell of a lot easier)and I have an Access DB with these entries in exported to CSV and txt formats. I thought Access started to fall over when it got any more than 10000 entries in it? I don't know.

The reason I would prefer to use either txt or csv is purely for updates sake. Say for arguments sake every friday this list was updated and the only way this list is published was a csv it would be easier just to overwrite the existing file rather than import it in to a DB. Also, I need to create DSNless connection which I have had errors for ever and a day with.

This is my first outing with PHP so I'm not really sure where I am going with to tbh :o)

dmorison

3:08 pm on Jan 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would just "try it", and see how slow it is. It all depends on the amount of data in each row.

The PHP is straight forward. I assume that you want an exact match on some field; so assume an incoming variable $match that you have retrieved from a form or whatever.

The following should point you in the right direction:

$match = "SOMEKEY"; // what we're looking for

$index = 0; // the field in which to look for $match, where the first value on a row of comma separated values is at index 0

$fp = fopen("/path/to/data.csv")

while($row = fgetcsv($fp,32768))
{
if ($row[$index] == $match)
{
$found = $row;
break;
}
}

fclose($fp);

if (is_array($found))
{
print "found it!";
print_r($found);
}
else
{
print "not found";
}

TonyL

3:36 pm on Jan 17, 2004 (gmt 0)

10+ Year Member



I get a parse error with that code?

I have some codewhich accessand displays every line but I really don't know enough to edit this?

Sorry I know I'm a classic newbie. :o)

<?

$fp = fopen("members.csv", "r");
$line_data = file("members.csv");
for($i=1;$i<count($line_data);$i++){

$line_elements = explode("¦", $line_data[$i]);
print trim($line_elements[0]) . '<br />';
print trim($line_elements[1]) . '<br />';

}

fclose($fp);

?>

TonyL

4:07 pm on Jan 17, 2004 (gmt 0)

10+ Year Member



ok, I am getting somewhere.

<?

$match = "002001"; // what we're looking for

$index = 0; // the field in which to look for $match, where the first value on a row of comma separated values is at index 0

$fp = fopen("fads.csv", "r");
$line_data = file("fads.csv");

while($row = fgetcsv($fp,32768))
{
if ($row[$index] == $match)
{
$found = $row;
break;
}
}

fclose($fp);

if (is_array($found))
{
print "found it!";

print_r($found);

}
else
{
print "not found";
}

?>

Returns an Array based on the ID number. How can I split this in to individual elements to populate an HTML form? I have tried:

print_r($found_elements[0]);

to separate out ID number, being the first column in the csv

TonyL

4:19 pm on Jan 17, 2004 (gmt 0)

10+ Year Member



It's all cool. I got it sorted now.

Cheers for pointing me in the right direction.