PHP- How to get the array of data from csv file

one csv file to store the array of data, and for each array am storing

Page 1 of 1

2 Replies - 13068 Views - Last Post: 09 September 2007 - 10:33 PM Rate Topic: -----

#1 nagarajsk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-September 07

PHP- How to get the array of data from csv file

Posted 08 September 2007 - 05:00 AM

CAn u tell me how to get the csv file data into php.. In csv file i have more than 1 row.. at each time i need one row to disply.. and again i have to take another row...
can u help me plz......

//For storing into csv file...
$
qty=$travells;
$prodID=$symbol;
$prodprice=$amount1;
$prodname=$inr;
$space='space';
	
					   
$out = $qty . "," . $prodID . "," . $prodprice . "," . $prodname . "\n";
	echo $out;
	$_file = 'show.csv';
	$_fp = @fopen( $_file, 'a' );
	@fwrite( $_fp, $out);
	@fclose( $_fp ); 

// to get into array...
<?php		
$filename = "show.csv";
											$fp=fopen($filename,"r");
									
$contents = fread($fd);
fclose($fp);
$delimiter = "\n";
											$splitcontents = explode($delimiter, $contents);
$counter = "";

foreach ( $splitcontents as $color )
{
$counter = $counter+1;
echo "<b>Split $counter: </b> $colorn<br>";
}


[mod edit] Added code tags.

Is This A Good Question/Topic? 0
  • +

Replies To: PHP- How to get the array of data from csv file

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4361
  • View blog
  • Posts: 12,180
  • Joined: 18-April 07

Re: PHP- How to get the array of data from csv file

Posted 08 September 2007 - 09:51 AM

One suggestion I have for you is to use the function "file()" which will read rows of a file straight into an array. Once in an array, you can loop through the array, exploding each one and do whatever you want to the elements. This might be a lot faster and even take less lines of code.

fread there is good for reading nice fluid streams of text etc and really depending on the file format could be the best choice, but it doesn't sound like it might be. If your file is formatted as such...

row1, 1, 2, hello, 4
row2, 3, 4, hello again, 5
row3, 8, 1, last hello, 2



Then you might want to try out the file() function. Quick example of its usage below...

<?php

// Open our test text file, read it into an array $fc
$fc=file("test.txt");

// Loop through the array, simply printing the line
foreach($fc as $line)
{
	  echo "Line is: $line";
	  // Here you could explode $line and get at the elements
}

?>



Hope this is along the lines you were looking for. :)
Was This Post Helpful? 0
  • +
  • -

#3 nagarajsk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-September 07

Re: PHP- How to get the array of data from csv file

Posted 09 September 2007 - 10:33 PM

View PostMartyr2, on 8 Sep, 2007 - 09:51 AM, said:

One suggestion I have for you is to use the function "file()" which will read rows of a file straight into an array. Once in an array, you can loop through the array, exploding each one and do whatever you want to the elements. This might be a lot faster and even take less lines of code.

fread there is good for reading nice fluid streams of text etc and really depending on the file format could be the best choice, but it doesn't sound like it might be. If your file is formatted as such...

row1, 1, 2, hello, 4
row2, 3, 4, hello again, 5
row3, 8, 1, last hello, 2



Then you might want to try out the file() function. Quick example of its usage below...

<?php

// Open our test text file, read it into an array $fc
$fc=file("test.txt");

// Loop through the array, simply printing the line
foreach($fc as $line)
{
	  echo "Line is: $line";
	  // Here you could explode $line and get at the elements
}

?>



Hope this is along the lines you were looking for. :)



Thanks for ur suggestion ... i ll try it
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1