0

So I have never wrote to a csv file before but always read from a csv file. Based on some example I have read this is what I have tried.

<?php
$newCsvData = array();
$getCats = "SELECT * FROM $termTax WHERE taxonomy = 'product_cat'";
$catresults = $wpdb->get_results($getCats);

foreach( $catresults as $catresult ) {
  $newCsvData[] = $catresult->term_id;
}
  $handle = fopen('export.csv', 'w');
  foreach ($newCsvData as $line) {
     fputcsv($handle, $line);
  }
  fclose($handle);
?>

I end up with errors saying fputcsv() expects parameter 2 to be array, string given in that is for this line

fputcsv($handle, $line);

If I var_dump($catresult); I get everything in the row as expected in an array... Example

object(stdClass)#401 (6) { ["term_taxonomy_id"]=> string(3) "224" ["term_id"]=> string(2) "37" ["taxonomy"]=> string(11) "product_cat" ["description"]=> string(0) "" ["parent"]=> string(1) "0" ["count"]=> string(1) "0" } object(stdClass)#402 (6) { ["term_taxonomy_id"]=> string(2) "35" ["term_id"]=> string(2) "35" ["taxonomy"]=> string(11) "product_cat" ["description"]=> string(0) "" ["parent"]=> string(1) "0" ["count"]=> string(1) "1" } 

I just don't know how to write the array to the csv...

If I var_dump($line) I get string(2) "37" each string and its the correct value.

Am I on the right path? Thanks in advance.

UPDATE: So I created a blank php file in the root of my wordpress install and used this code and it works find... well some code someone below fixed... But the thing is it will not work from within my plugin I am working on.

2
  • what values does var_dump($newCsvDat) return ? Commented May 7, 2014 at 23:28
  • It's clear from the code. It's array, each field contains $catresult->term_id as a string. Commented May 7, 2014 at 23:33

2 Answers 2

2

The error message is telling you what is wrong.

fputcsv() expects parameter 2 to be array

And from your own admission (emphasis mine)

If I var_dump($line) I get string(2) "37" each string and its the correct value

Why not try this? You're building an array already.

foreach( $catresults as $catresult ) {
    $newCsvData[] = $catresult->term_id;
}
fputcsv($handle, $newCsvData);
Sign up to request clarification or add additional context in comments.

5 Comments

Yep, I agree, that it's more than clear. But everyone has to start somehow :)
This way he'll have his data in columns, not rows.
I tried that thanks. Maybe its just not writing for some reason... I no longer get those errors though. I was doing something similar earlier without errors but still was not writing to the csv file.
I wasn't entirely sure how he wanted the CSV dumped out so I went with the simplest approach, mainly to demonstrate using the array vs strings
GRRRRR... the whole time it was working. But since it was inside of a plugin, the path ended up sending the file to wp-admin and not my plugin directory...
1

The error messages are so clear that there is nothing better to say.

Simply create each line ($newCsvData array field) as array:

$newCsvData[] = array($catresult->term_id);

This way you will have each line in a CSV file with 1 column.

If you would like to have a CSV file with for example 3 columns, use this:

$newCsvData[] = array(
    $catresult->term_id,
    $catresult->another_filed,
    $catresult->yet_another_field
);

3 Comments

Thanks for the suggestion but its not writing to the csv file.
What is not writing to the CSV file?
From manual: int fputcsv ( resource $handle , array $fields [, string $delimiter = "," [, string $enclosure = '"' ]] ) - it puts one line of fields of the $fields array (columns) to the CSV file. What does it mean "its not not writing"? And what is not writing? Paste your code. Post your errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.