Link to home
Start Free TrialLog in
Avatar of jillette
jilletteFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Select one line from a csv

Hi,

how do you select one line from a csv file to be displayed on a web page?

thanks

Rob
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
At php.net, you will find a good function to read CSV file into array. Then, the inly thing you ave to do is select only the first row...

http://php.net/manual/en/function.file.php (look for [twichi at web dot de] 13-Sep-2011 12:33 :: csv_in_array)
@fundacionrts - I would not recommend doing this for just reading one line.

The file() function will read in the entire file, which is very inefficient when all you need is one line (and it won't process CSV fields, either). On a web page, where you usually have many people accessing it, this can lead to a lot of overhead processing. If you have a big CSV file, then it requires even more processing power and will hit the hard drive a lot more.
Thanks for the advice gr8gonzo.
Avatar of jillette

ASKER

i what the ability to select any line, not just the first line
@jillette: How will you know what line you want to select?  Please post a small sample CSV file and tell us what line you want, and why.  Then we can show you the code that will choose that line.  Thanks, ~Ray
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
While my code should work fine in many cases, if you're working with large CSV files, you may need something better. For example, if you're working with a 50-megabyte CSV file, it may take several seconds just to skip down to the 130,000th line so you can print it. There are ways to help this, but it takes more planning and understanding the process (knowing or detecting the proper offsets, knowing the file structure, etc...)

If you're only working with small CSV files, the code above should work fine. If you regularly retrieve the same line over and over again (especially if it's a line near the end of the file), it may be better to cache it in a separate file so you don't have to read through the file to get to the desired line.
However if you are dealing with a 50MB csv file you should probably consider using a database instead of a csv. In fact I would go as far as to say if you're csv is 1MB then its time to move to a DBMS.
I've requested that this question be deleted for the following reason:

no real solution
Go ahead and delete the question if you want.  But going forward, please respond to the Experts' questions.  For example, see the question at ID: 37774012.  Had you answered that I am almost certain you would have gotten an excellent solution, probably including a tested and working demonstration script.

Best of luck with your project, ~Ray
Why is there no real solution? I provided a function that does what you're asking for - if it doesn't work, let us know why or how.