Solved

Select one line from a csv

Posted on 2012-03-27
14
219 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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 109

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
 
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 109

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Google Maps API and PHP 25 34
two ways encryption with php 3 28
website maintenance mode 1 17
What does this formatting equate to? 7 12
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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

803 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