Solved

Select one line from a csv

Posted on 2012-03-27
14
217 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
Comment Utility
<?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
Comment Utility
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
Comment Utility
@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
Comment Utility
Thanks for the advice gr8gonzo.
0
 

Author Comment

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

Expert Comment

by:Ray Paseur
Comment Utility
@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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 34

Assisted Solution

by:gr8gonzo
gr8gonzo earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I've requested that this question be deleted for the following reason:

no real solution
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

762 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

5 Experts available now in Live!

Get 1:1 Help Now