0

Ive looked around and ive found similar examples and gave them a shot but I can't get mine to work...

Here is mine...It doesnt work..What am I doing wrong :S? The type of POST is arrays so I guess I have to convert it to string to make it work.. The names and numbers look like this: Array ( [0] => john Hartz [1] => Cindy Cinamon [2] => Fruit Cake ) Array ( [0] => 9058553699 [1] => 4167641345 [2] => 4167641543 )

<?php
error_reporting(-1);


$list = array (
    $_POST['names'],
    $_POST['numbers']
);

$fp = fopen('numbers.csv', 'w');

foreach ($list as $fields) {
    fputcsv($fp, $fields);
}

fclose($fp);
?>

The below works...

$list = array (
    array('aaa', 'bbb', 'ccc', 'dddd'),
    array('"aaa"', '"bbb"')
);

$fp = fopen('file.csv', 'w');

foreach ($list as $fields) {
    fputcsv($fp, $fields);
}

fclose($fp);
?>

here is the more new version, somewhat working but not quite...

<?php
error_reporting(-1);


$name = implode(",", $_POST['names']);
$num= implode(",", $_POST['numbers']);
$list = array (
    array($name, $num)
);

$fp = fopen('numbers.csv', 'w');

foreach ($list as $fields) {
    fputcsv($fp, $fields);
}

fclose($fp);
?>

brady, the problem isnt formatting....You see where you have "|" between names or numbers.... that im guessing shows the border between 1 cell and the other....well what I am getting is an ENTIRE array into ONE cell...So something like this...

|"john Hartz"  "Cindy Cinamon" "Fruit Cake"|

---------------------------------------

| 905855369941676413454167641543 |
Bulvak
  • 1,794
  • 8
  • 31
  • 63
  • Are `$_POST['names']` and `$_POST['numbers']` strings or arrays? – Londeren Jul 19 '11 at 19:32
  • 1
    The code snippets are exactly the same. How does the first one not work? Have you inspected $_POST? Is there a permissions issue blocking write to numbers.csv? – Mr Griever Jul 19 '11 at 19:34
  • What error did you get? Wrong data written to file, not written at all? PHP error? – Timur Jul 19 '11 at 20:05
  • can you define, `It doesnt work`? File not being saved, file saved but empty, file saved but wrong data. Have you looked at the document in plain text? Please add a `var_dump` of the `$_POST` – brady.vitrano Jul 19 '11 at 20:06
  • I'll second permissions if `$fields` is definitely an array for each iteration. You would get a PHP Warning if you fed a non-array into `fputcsv` anyway. Maybe create the file first, or try and see if it works in `/tmp/numbers.csv` – Darragh Enright Jul 19 '11 at 20:09
  • http://imageshack.us/photo/my-images/807/csv.png/ – Bulvak Jul 19 '11 at 20:35

2 Answers2

1

I am going to take a wild guess and assume your file is writing and your referring to the format. I assume this is what you have

"john Hartz" | "Cindy Cinamon" | "Fruit Cake"
---------------------------------------
9058553699   | 4167641345      | 4167641543

And you want this

"john Hartz"    | 9058553699  
----------------------------
"Cindy Cinamon" | 4167641345  
----------------------------
"Fruit Cake"    | 4167641543

If that is the problem, then try this:

    $_POST['names']   = array('john Hartz', 'Cindy Cinamon', 'Fruit Cake');
    $_POST['numbers'] = array(9058553699  , 4167641345  ,4167641543);

    $fp = fopen('file.csv', 'w');
    $fields = array();
    for ($i = 0; $i < count($_POST['names']); $i++) {
        $fields = array($_POST['names'][$i], $_POST['numbers'][$i]);
        fputcsv($fp, $fields);
    }

    fclose($fp);

EDIT

Have you tried getting rid of the foreach loop and the $list array and just try this :

fputcsv($fp, $_POST['names']); 
fputcsv($fp, $_POST['numbers']); 
brady.vitrano
  • 2,256
  • 2
  • 16
  • 26
  • i get this error after i tried what you said: Warning: fputcsv() expects parameter 2 to be array, string given in C:\Program Files\xampp\htdocs\xampp\something.php on line 15 Warning: fputcsv() expects parameter 2 to be array, string given in C:\Program Files\xampp\htdocs\xampp\something.php on line 15 – Bulvak Jul 20 '11 at 13:49
  • Can you show us the results of `var_dump($_POST['names'])`, if it is an array as you have said it is, then the my answer should work. – brady.vitrano Jul 20 '11 at 14:35
0

Side note: try doing error_reporting(E_ALL) instead of -1. There have been some changes to the values (etc) for error_reporting flags. Perhaps it'll give you a warning that is helpful

Will Bonde
  • 560
  • 6
  • 19
  • 1
    The problem occuring is a semantic/logical one not syntax.... The issue is that 1 array is being put in 1 cell of the csv and the other array is being put into the other cell....I want each item to have its own cell and each array to have its own column.. – Bulvak Jul 19 '11 at 20:07