[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

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
0
jillette
Asked:
jillette
  • 5
  • 2
  • 2
  • +2
2 Solutions
 
gr8gonzoConsultantCommented:
<?php

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

?>
0
 
fundacionrtsAdministrador de SistemasCommented:
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
 
gr8gonzoConsultantCommented:
@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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
fundacionrtsAdministrador de SistemasCommented:
Thanks for the advice gr8gonzo.
0
 
jilletteAuthor Commented:
i what the ability to select any line, not just the first line
0
 
Ray PaseurCommented:
@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
 
gr8gonzoConsultantCommented:
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
 
gr8gonzoConsultantCommented:
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
 
DerokorianCommented:
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
 
jilletteAuthor Commented:
I've requested that this question be deleted for the following reason:

no real solution
0
 
Ray PaseurCommented:
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
 
gr8gonzoConsultantCommented:
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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 5
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now