How to add columns to CSV using PHP - php

I need to add columns to an existing csv file ,but i can't find any solution to the problem.I have used "\t" and chr(9) to create columns but no success so please help me by providing me the right solution if any one can

Try this, and have a look at fgetcsv() and fputcsv() in the manual
<?php
$newCsvData = array();
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$data[] = 'New Column';
$newCsvData[] = $data;
}
fclose($handle);
}
$handle = fopen('test.csv', 'w');
foreach ($newCsvData as $line) {
fputcsv($handle, $line);
}
fclose($handle);
?>

Could you try using \r\n instead of \n ?

Related

How to do category mapping from csv?

I have a csv form which I have to make the category mapping. I had let this csv read from controller.And, I am trying to establish a hierarchy .So, that I can map . But , I am not able to do that. Please help!
[link:https://pricefalls.zendesk.com/hc/en-us/articles/204287645-How-do-I-create-a-category-map- “Pricefalls Category Taxonomy”].My csv has the same pattern as shown in the given link.
public function actionReadcsv()
{
$row = 1;
$csvFile = Yii::getAlias('#frontend') . '/modules/pricefalls /csv/Pricefalls_Categories1.csv';
if (($handle = fopen($csvFile, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, "|")) !== FALSE) {
$row++;
$create_category[] =$data;
}
Pricefallscategories::categorytree($create_category);
echo "<pre>";
print_r($create_category);
echo"</pre>";
fclose($handle);
}
}

PHP Append Column to end of the CSV file

I have a CSV file in the php server
Want to Add "h3" at the end and fill the row with data "data3"
here is the structure
H1 H2 (h3)
XXX XXX data3
xxx xxx data3
I am trying with the following code for a long time, havent get any luck yet
<?php
$newCsvData = array();
if (($handle = fopen("file.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$data[] = 'report_type';
$newCsvData[] = $data;
}
fclose($handle);
}
$handle = fopen('test.csv', 'w');
foreach ($newCsvData as $line) {
fputcsv($handle, $line);
}
fclose($handle);
?>
Please advice

Get string entry from textfile in php

I have a question. How can i get a string from a entry in a textfile.
The texfile looks like:
Blabla2|123456
Blablablabla4|475387638
Blab1|387549900
Now i want to get from Blablablabla4 the number behind the |
Whats the read file function for it?
You can create a custom function, if you need that value often with different keys.
function getValueFromFile($file, $key){
if (($handle = fopen($file, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, "|")) !== FALSE) {
if($data[0] == $key){
fclose($handle);
return $data[1];
}
}
fclose($handle);
}
return false;
}
and simply call it like:
echo getValueFromFile("your file name", "Blablablabla4");
Load each line of file to $line and then use function explode('|', $line_variable) for separating a string from int.
$handle = fopen("inputfile.txt", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
$line = explode('|', $line);
$string = $line[0];
}
fclose($handle);
} else {
// error opening the file.
}

How to fetch specific column from a file?

I am trying to fetch a specific column from a file by using PHP. but so far I got all of it, have no idea how to do this, I can not find any information online so if anyone could help me about this , I will be very appreciated. The data in file is like blow:
20110101,1110.0
20110102,1100.0
20110103,50.0
20110104,6355.0
........
I just want to fetch the second column, here is my PHP code:
$file_handle = fopen("file_name", "r");
while (!feof($file_handle)) {
$line = fgets($file_handle);
echo $line."<br/>";
}
fclose($file_handle);
Have you tried this (which actually does what you want, without refactoring) :
$file_handle = fopen("file_name", "r");
while (!feof($file_handle)) {
$line = fgets($file_handle);
$cols = explode(',', $line);
echo $cols[1]."<br/>";
}
fclose($file_handle);
??
As Vlad says, use fgetcsv.
// adapted from PHP example
$max_row_len = 1000;
if (($file_handle = fopen("file_name", "r")) !== FALSE) {
while (($data = fgetcsv($file_handle, $max_row_len, ",")) !== FALSE) {
echo $data[1]."<br/>";
}
fclose($file_handle);
}

How do i remove the top line in a CSV file (the coloumn headers)?

I have put together a script which will upload a CSV file and then extract the data into an already made table. I want to make it so the first line(the column headers) will not be inserted into the table, but the rest of the data will be.
$fp = fopen($_SESSION['filename'],"r");
while (($data = fgetcsv($fp, 1000, ",")) !== FALSE)
{
$import="INSERT into csv_table(name,address,age) values('$data[0]','$data[1]','$data[2]')";
mysql_query($import) or die(mysql_error());
}
fclose($fp);
this is the part of the code i use to extract the data from the csv file.
Thank You very much for any help with this matter!
Just put the following before the while loop to read the first line:
fgetcsv($fp, 1000, ",");
Thereafter the while loop starts with the second line instead.
Underthink it.
Create a boolean flag on the outside, and toggle it once you enter the loop instead of importing, using an if statement.
Simply do a blank read as such:
$fp = fopen($_SESSION['filename'],"r");
$headerLine = true;
while (($data = fgetcsv($fp, 1000, ",")) !== FALSE)
{
if($headerLine) { $headerLine = false; }
else {
$import="INSERT into csv_table(name,address,age) values('$data[0]','$data[1]','$data[2]')";
mysql_query($import) or die(mysql_error());
}
}
fclose($fp);
You can use out of loop fgetcsv() function and then, after your array, print value second line:
if (($handle = fopen($target_file, "r")) !== FALSE) {
$i=1;
fgetcsv($handle, 1000, ",");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
echo "<pre>";
print_r($data);
echo "</pre>";
$i++;
}
}

Categories