Solved

Select one line from a csv

Posted on 2012-03-27
14
218 Views
Last Modified: 2012-04-15
Hi,

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

thanks

Rob
0
Comment
Question by:jillette
  • 5
  • 2
  • 2
  • +2
14 Comments
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 37773844
<?php

if (($handle = fopen("test.csv", "r")) !== FALSE) {
 
   if($data = fgetcsv($handle, 1000, ","))
   {
       print_r($data);
   }
   fclose($handle);
}

?>
0
 
LVL 8

Expert Comment

by:fundacionrts
ID: 37773857
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)
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 37773887
@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.
0
 
LVL 8

Expert Comment

by:fundacionrts
ID: 37773897
Thanks for the advice gr8gonzo.
0
 

Author Comment

by:jillette
ID: 37773923
i what the ability to select any line, not just the first line
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37774012
@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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 34

Assisted Solution

by:gr8gonzo
gr8gonzo earned 500 total points
ID: 37774013
Hi jilette,

That's fine - you can skip lines, too. I expanded that code into a reusable function.

<?php

// Get and print out line 5 of test.csv
$lineFive = getLineFromCSV(1,"test.csv");
print_r($lineFive);

// Get and print out line 3 in other.csv (shorter version)
print_r(getLineFromCSV(3,"other.csv"));

function getLineFromCSV($lineNumber,$file)
{
	$currentLine = 1;
	
	if (($handle = fopen($file, "r")) !== FALSE)
	{
		// Skip to desired line
		while($currentLine != $lineNumber)
		{
			$line = fgetcsv($handle, 1000, ",");
			$currentLine++;
			
			// Return last line if we hit the end of the file
			if(feof($handle))
			{
				return $line;
			}
		}
		
		// Get line
		if($line = fgetcsv($handle, 1000, ","))
		{
			return $line;
		}
		
		fclose($handle);
	}
	else
	{
		// Couldn't open the file so throw an error
		echo "Could not open {$file}!";
		return false;
	}
}

?>

Open in new window


Code moved to code tags by ModernMatt, 15th April 2012, to reduce length of question page
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 37774033
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.
0
 
LVL 10

Expert Comment

by:Derokorian
ID: 37776837
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.
0
 

Author Comment

by:jillette
ID: 37788773
I've requested that this question be deleted for the following reason:

no real solution
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37788195
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
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 37788774
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now