formatting fgetcsv output - php

Im trying to figure out how to take the data returned by fgetcsv, and format it into a readable/editable table, and then use fputcsv to save that table
so far i have this
<?php
$row = 1;
$handle = fopen("csv.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "\n";
}
}
fclose($handle);
?>

The following code will output the CSV in an html table. To make it editable wrap the echo ..$val.. with tags and add a php form handler that takes the result and reforms a CSV
<?php
$row = 1;
$handle = fopen("csv.csv", "r");
echo("<table>");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
echo("<tr>\r\n");
foreach ($data as $index=>$val) {
echo("\t<td>$val</td>\r\n");
}
echo("</tr>\r\n");
}
echo("</table>");
fclose($handle);
?>

Related

Saving specific columns of CSV to another CSV file in PHP

so trying to read a csv file and extracting details from the file and wish to store specific columns of the csv to another csv file.
I have managed to extract the specific columns of the input csv file but not really sure how I can write those extracted columns to another csv file.
Here is the part of my work:
$row = 1;
if (($handle = fopen("testtable.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
echo $data[0] . "\n" . $data[1] . "<br />\n";
}
$file = fopen("editedTable.csv","w");
foreach ( $data as $line) { //working on this part of the code
fputcsv($file, explode(',',$line));
}
fclose($handle);
}
Obviously there is an error in the foreach loop as I am trying to work out the logic.
The data that is extracted is basically first and the second column (data[0], data[1]). Just need this to write to another CSV file. The input csv is just a comma delimited file with 5 columns. Any idea how I can modify the foreach loop and the use the fputcsv method?
Thanks
fputcsv accepts an array on the second parameter.
I think this is what you looking for:
<?php
$file = fopen("editedTable.csv","w");
$row = 1;
if (($handle = fopen("testtable.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000)) !== FALSE) {
$num = count($data);
$row++;
echo $data[0] . "\n" . $data[1] . "<br />\n";
fputcsv($file, [$data[0], $data[1]]);
}
}
fclose($handle);
fclose($file);
Because you place the fputs outside of the $data initial loop, you catch only one row, here is the modified code.
$row = 1;
$file = fopen("editedTable.csv","w");
if (($handle = fopen("testtable.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
echo $data[0] . "\n" . $data[1] . "<br />\n";
$line = $data[0].", ".$data[1];
$line .= "\n";
fputs($file, $line);
}
fclose($handle);
}
fclose($file);

PHP changing the index of multidimentional array to value of variable

Pretty new to PHP. I have a csv feed that i am trying to display every line of in the right place. The feed is loaded into a multidimentional array and I can echo out specific places like this.
echo $readcsv[0][2]
But I am trying to use the value of the variable $row in stead of the first number while doing a while loop.
Something like:
echo $readcsv[$row][2]
I have tried next(), str_replace() and strtr() but none of them seems to work while the loop is running.
$row = 1;
$readcsv = array();
if (($handle = fopen("file.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$num = count($data);
echo $readcsv[$row][2];
$row++;
for ($c=0; $c < $num; $c++) {
}
$mycsvfile[] = $data;
}
fclose($handle);
}
CSV file:
title;image_link;link;empty;price;
1;back.gif;http://link.com;;19.95;
2;back.gif;http://link.com;;19.95;
3;back.gif;http://link.com;;19.95;
4;back.gif;http://link.com;;19.95;
5;back.gif;http://link.com;;19.95;
I am not quite sure from your code and question exactly what you are trying to do, but maybe its something like this
if (($handle = fopen("file.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
// you load the line into $data and its a one dimentional array
// occurance 2 is the `link` (http://link.com) if that what you want to echo
echo $data[2];
// as you have a loop, maybe you were trying to
// echo each fields from the csv so you can do that like this
foreach ($data as $idx => $value) {
echo "Column $idx = $value";
}
$mycsvfile[] = $data;
}
fclose($handle);
}

PHP open csv and save data to new csv

I need to replace a lot of data in csv and after changing all things i want it saved to a new csv file.
i can read the csv with the code from php.net but i can't get it to work saving the new file. i have this
$row = 0;
if (($handle = fopen("original.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 20000, ";")) !== FALSE) {
$num = count($data);
$cat_alt = ['cold', 'hot', ... and so on...];
$cat_neu = ['kalt', 'heiß', ...und so weiter...];
echo "<p> $num Felder in Zeile $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
$output = str_replace($cat_alt, $cat_neu, $data[$c] . "<br />\n");
echo $output;
}
}
}
$fp = fopen('changed.csv', 'w');
foreach ($row as $rows) {
fputcsv($fp, $rows);
}fclose($fp);
You have two options:
Use n array
$rows = []
...
$rows[] = str_replace($cat_alt, $cat_neu, $data[$c] . "\n");
...
fputcsv($fp, $rows);
Open the output file beforehand and write to it as you process the data
$fp = fopen('changed.csv', 'w');
if (($handle = fopen("original.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 20000, ";")) !== FALSE) {
$num = count($data);
$cat_alt = ['cold', 'hot', ... and so on...];
$cat_neu = ['kalt', 'heiß', ...und so weiter...];
echo "<p> $num Felder in Zeile $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
fputs($fp, str_replace($cat_alt, $cat_neu, $data[$c] . "<br />\n"));
}
}
}
fclose($fp);

PHP - parse a csv file and write output to another csv file

I am working on Magento products csv file and i have a csv file with 3 columns like
<pre>Name, description, Color</pre>
<pre>t-shirt, tshirt description, "green, blue, yellow"</pre>
Now based on the values of column 3rd column (color) i want to output each row in a separate file say "new_products.csv" in a manner so the resultant file would be like
<p>Name, description, Color</p>
<pre>t-shirt, tshirt description, green</pre>
<pre>t-shirt, tshirt description, blue</pre>
<pre>t-shirt, tshirt description, yellow</pre>
i have started with this code
$row = 1;
if (($handle = fopen("product.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$name = $data[0];
$desc = $data[1];
for ($c=0; $c < $num; $c++) {
$fp = fopen('new_products.csv', 'w');
if($c == 2){ //if we are at color field
$color = explode(',', $data[$c]);
$color_count = count($color);
for($i=0; $i<$color_count; $i++){
fputcsv($fp, array($name,$desc,$color[$i]));
}
}
}
$row++;
}
fclose($handle);
}
However the above code only outputting the last row only.
Thanks
Finally i have been able to find the solution my self. Actually one need to set the file pointer at the end of the file.
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$name = $data[0];
$desc = $data[1];
for ($c=0; $c < $num; $c++) {
if($c == 2){ //if we are at color field
$fp = fopen('file.csv', 'a'); //Open for writing only; place the file pointer at the end of the file.
$color = explode(',', $data[$c]);
$color_count = count($color);
for($i=0; $i<$color_count; $i++){
fputcsv($fp, array($name,$desc,$color[$i]));
}
fclose($fp);
}
}
$row++;
}
fclose($handle);
}

How to store each row of the csv file to array in php

I have data in csv file as follows.
Mr,Vinay,H S,Application Engineer,vinay.hs#springpeople.com,99005809800,Yes
Mr,Mahammad,Hussain,Application Engineer,vinay.hs#springpeople.com,99005809800,Yes
I want to store each row in each array.explain me how to do that
I did not understand each row in each array.
use fgetcsv() function to read the CSV file php.
An example
<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
?>
OR
If you want to store them in an array you could use
$data = array();
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while(($row = fgetcsv($handle, 1000, ",")) !== FALSE) {
$data[] = $row;
}
}
Assuming the CSV is a file (if not, make the CSV match the $csv, which is essentially the file broken down in the separate lines), here is a very simple way:
$csv = file('mycsv.csv'); $result = array();
//iterate the lines
foreach ($csv as $line)
// I'd recommend more robust "csv-following" method though (fgetcsv maybe)
$result[] = explode(',',$result);
//show the results
print_r($result);
Though fgetcsv is probably a safer bet (as others have mentioned) (See the docs).
To further extend your comment:
//iterate the lines
foreach ($csv as $line)
// I'd recommend more robust "csv-following" method though (fgetcsv maybe)
$result[] = explode(',',$result);
can become:
//iterate the lines
foreach ($csv as $line)
// combine the current results with the new results
$result = array_marge($result,$line);
or, if order is important:
//iterate the lines
foreach ($csv as $line)
// add each result on to the end of the result array
foreach($line as $val)
// add each value
$result[] = $val;
You can use fgetcsv() for that:
$data = array();
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while(($row = fgetcsv($handle, 1000, ",")) !== FALSE) {
$data[] = $row;
}
}

Categories