Array write to CSV - php

I have an array that I want to write to a CSV file and tried the fputcsv function following the example on the manual.
However I am getting the error "Fatal error: Call to undefined function fputcsv()"
$csv = array();
foreach($compare as $ukvalue)
{
$csv[] = array($ukvalue, $uk[$ukvalue]);
}
$fp = fopen("lang.csv", "w");
foreach ($csv as $fields)
{
fputcsv($fp,explode(',', $fields));
}
fclose($fp);
Can anyone shed some light on this issue or is there an alternative to fputcsv I can try?

As per the docs at http://www.php.net/manual/en/function.fputcsv.php the function putcsv is available only for PHP 5.1.0 or later
Which version oh PHP are you running?
Also, you should not explode the fields, in fact the expected second parameter should be an array.

Related

Can't get a csv file using fputcsv() and an array returned by pg_fetch_xxx()

I am trying to export a csv file with php, using an array returned by pg_fetch_array. But the csv file is empty.
If I write an array by hand, the csv is correct.
I tried to build an array from pg_fetch_all without success.
<?php
$listope = pg_fetch_array($marequete) ;
$fichier = fopen("export.csv", "w") ;
foreach ($listope as $line) {
fputcsv($fichier, explode(',',$line));
}
fclose($fichier) ;
?>
I know that the problem is the $listope var which does not return a proper array but I can't find the solution.
Thanks for help
Dont explode the array, the fputcsv() function does all that for you
So simply do
fputcsv($fichier, $line);
Another thing, you only read one row from the result set, now that maybe because you only expect one row, but as you also code a loop, if the query would generate more than one row try this, using pg_fetch_all()
$listope = pg_fetch_all($marequete) ;
$fichier = fopen("export.csv", "w") ;
foreach ($listope as $line) {
fputcsv($fichier, $line);
}
fclose($fichier) ;
Reference The manual
I think you are making this more complicated than necessary. pg_fetch_array() returns one row from your result-set, so it you want to save that in a csv file, you would only need:
$listope = pg_fetch_array($marequete) ;
$fichier = fopen("export.csv", "w") ;
fputcsv($fichier, $listope);

How to change all values in a column of a csv file to a specific value php

I have a csv file that looks something like this (there are many more rows):
Jim,jim#email.com,8882,456
Bob,bob#email.com,8882,343
What I want to do is to change all the values in the fourth column,456,343 to 500.
I'm new to php and am not sure how to do this.
I have tried
<?php
$file = fopen('myfile.csv', 'r+');
$toBoot = array();
while ($data = fgetcsv($file)) {
echo $data[3];
$data[3] = str_replace($data[3],'500');
array_push($toBoot, $data);
}
//print_r($toBoot);
echo $toBoot[0][3];
fputcsv($file, $toBoot);
fclose($file)
?>
But it prints
Jim,jim#email.com,8882,456
Bob,bob#email.com,8882,343
Array,Array
not
Jim,jim#email.com,8882,500
Bob,bob#email.com,8882,500
I've looked at this post, PHP replace data only in one column of csv but it doesn't seem to work.
Any help appreciated. Thanks
You can use preg_replace and replace all values at once and not loop each line of the CSV file.
Two lines of code is all that is needed.
$csv = file_get_contents($path);
file_put_contents($path, preg_replace("/(.*),\d+/", "$1,500", $csv));
Where $path is the path and to the CSV file.
You can see it in action here: https://3v4l.org/Mc3Pm
A quick and dirty way to way to solve your problem would be:
foreach (file("old_file.csv") as $line)
{
$new_line = preg_replace('/^(.*),[\d]+/', "$1,500", $line);
file_put_contents("new_file.csv", $new_line, FILE_APPEND);
}
To change one field of the CSV, just assign to that array element, you don't need to use any kind of replace function.
$data[3] = "500";
fputcsv() is used to write one line to a CSV file, not the entire file at once. You need to call it in a loop. You also need to go back to the beginning of the file and remove the old contents.
fseek($file, 0);
ftruncate($file, 0);
foreach ($toBoot as $row) {
fputcsv($file, $row);
}

cannot redeclare function - trying to call function every X seconds - php

I've got a php problem.
I've got a php file that reads data from a .txt file.
This works, with this code:
$filename= "deadlines.txt";
$fp = fopen($filename,"r");
$content = fread($fp, filesize($filename));
$rawArray = setRawArray($content);
$epochAndTitleArray = toEpoch($rawArray);
Now, I want to make it so that this stuff is executed every second, not just once at the start.
So, I tried to fit it into a function, like this:
$filename= "deadlines.txt";
$fp = 0;
$content = 0;
$rawArray = 0;
$epochAndTitleArray = 0;
function readFile(){
$GLOBALS['fp'] = fopen($GLOBALS['filename'], "r");
$GLOBALS['content'] = fread($GLOBALS['fp'], filesize($GLOBALS['filename']));
$GLOBALS['rawArray'] = setRawArray($GLOBALS['content']);
$GLOBALS['epochAndTitleArray'] = toEpoch($GLOBALS['rawArray']);
}
In this case I'm working with globals, before, I did it without, and also left out the lines before the function itself. This was incorrect I think, so I added the globals.
Now, this doesn't work.
It gives me the following error:
Fatal error: Cannot redeclare readFile() in .....on line 28,
this line 28 is the line of the closing } at the end of the function.
Can you guys help me in completing this task?
Thanks already!
readfile is a defined function in php , you cannot redeclare it or redeclare any function using the same name .
for more reference about how to declare valid functions in php
PHP does not support function overloading, nor is it possible to
undefine or redefine previously-declared functions.
Rename 'readFile' to another, readfile() is predefined function 'http://php.net/manual/kr/function.readfile.php'

Create csv file from mysql results with php?

So I'm trying to make a .csv file which after I will download it,but the problem is that the rows are not going aranging properly.
I am using fputcsv.
$tot.="$codcomanda,$clientnume,$brandnume,$numeprod,$um,$cantitate,$updatare";
$list =array(
array('Comanda','Client','Categorie','Produs','UM','Cantitate','Actualizare'),
array($tot),
);
$fp = fopen('file.csv', 'w');
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
The variable $tot is from a while statement and it gets its variable from different queries and I cannot find a php code on the internet that is for my needs.I've tried several ways to try and make it work but nothing.
I've tried making $tot different ways but none work,either it $tot.=
The csv file should have something like.
Title line,
Line 1,
Line 2,
etc
But my csv shows like
Title line,
"Line1,Line2,etc"
I've tried making $tot different ways but none work,either the csv show like this or doesn't display no content at all.
Solved: Using fwrite i've solved it.
$tot.="$codcomanda;$clientnume;$brandnume;$numeprod;$um;$cantitate;$updatare;\n";
$tot2="Comanda;Client;Brand;Produse;U.M;Cantitate;Actualizare;\n$tot";
$file = fopen("file.csv","w");
fwrite($file,$tot2);
fclose($file);
And it write's it like it should.
Try like the following example
<?php
$list =array(
array('Comanda','Client','Categorie','Produs','UM','Cantitate','Actualizare'),
array('P1','C1','CL1','2','2','Ct1','A1'),
array('P2','C2','CL2','2','2','Ct2','A2'),
);
$fp = fopen('file.csv', 'w');
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp)
?>
Try to add this line before the calling of fopen(): ini_set("auto_detect_line_endings", true)

How can I work with .LIST file using Javascript or PHP?

I have a .list file that I would like to use as an array either with JS or PHP. Is there a way to do this?
Quick Solution
<?php
$fp = fopen('path/to/your/file.list', 'r');
$array = array();
while( ! feof($fp))
{
$array[] = fgets($fp, 1024);
}
fclose($fp);
print_r($array);
?>
This will create an array with each entry being one line of your .list file.
Further Reading
If you want more information, such as error handling, you can check out this php manual page on fgets() it has code examples on there.
You may also be interested in fgetcsv() which also has code examples

Categories