Solved

Select one line from a csv

Posted on 2012-03-27
14
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +2
14 Comments
 
LVL 35

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 35

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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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 110

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 35

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 35

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 110

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 35

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

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.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

730 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