I am trying to import data from a db via pdo and output the results to a csv file. I am able to output to a screen correctly but the formatting in the csv is wild, double names and no '\n' 

```
<?php
require_once('auth.php');
$conn = new PDO("mysql:host=localhost;dbname=$dbname", $username, $pw);

if (($handle = fopen("nameList2.txt", "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, "        ")) !== FALSE) {
                $firstname = $data[0];
                $lastname = $data[1];
                $stmt = $conn->prepare("SELECT * FROM list WHERE FName = :firstname AND LName = :lastname");
                $stmt->bindParam(':firstname', $firstname);
                $stmt->bindParam(':lastname', $lastname);
                $stmt->execute();
                $result = $stmt->fetchAll();
                //var_dump($firstname);
                //var_dump($lastname);
                //var_dump($result);
                $fp = fopen('file.csv', 'w');
                foreach($result as $chunk){
                        echo $chunk[4]."        ".$chunk[6]."   ".$chunk[7]."   ".$chunk[10]."  ".$chunk[11]."".$chunk[12]."  ".$chunk[13]."  ".$chunk[18]."  ".$chunk[19]."  ".$chunk[20]."<br />";
                        fputcsv($fp, $chunk);
                }
                fclose($fp);
        }
        fclose($handle);
        //fclose($fp);
}

?>

```

![enter image description here](https://i.stack.imgur.com/6v80n.png)
['https://Stackoverflow.com/questions/23020571', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2528910/']
I've [updated your fiddle](http://jsfiddle.net/8s9ss/4) and below code as well. I hope that will help.

Regards

```
var app = angular.module('app', ['ui.bootstrap']);

app.controller('ModalInstanceCtrl', function ($scope, $modalInstance, customer)
{
$scope.customer = customer;

});

app.controller('CustomerController', function($scope, $timeout, $modal, $log) {

    $scope.customers = [
        {
        name: 'Ricky',
        details: 'Some Details for Ricky',
        },
        {
        name: 'Dicky',
        details: 'Some Dicky Details',
        },
        {
        name: 'Nicky',
        details: 'Some Nicky Details',
        }
    ];

    // MODAL WINDOW
    $scope.open = function (_customer) {

        var modalInstance = $modal.open({
          controller: "ModalInstanceCtrl",
          templateUrl: 'myModalContent.html',
            resolve: {
                customer: function()
                {
                    return _customer;
                }
            }
             });

    };

});

```
This is how I setup my modals for handleing items that I ng-repeat over and want to edit. I suggest setting it up to work with a different controller, because then you can use DI to inject the resolved item to the child scope.

```
$scope.openModal = function(item) 
    // This sets up some of the options I want the modal to open with
    var options = {}
    angular.extend(options, {
      templateUrl: '/views/userItems/form.html',
      controller: 'ItemEditController',
      resolve: {
        // I resolve a copy of the so it dont mess up the original if they cancel
        item: function() { return angular.copy(item); }
      }
    });
    $modal.open(options).result.then(function(updatedItem) {
      // after the user saves the edits to the item it gets passed back in the then function
      if(updatedItem) {
        // this is a service i have to deal with talking to my db
        modelService.editItem(updatedItem).then(function(result) {
          // get the result back, error check then update the scope
          if(result.reason) {
            $scope.addAlert({type: 'error', title: 'Application Error', msg: result.reason});
          } else {
            angular.extend(vital, result);
            $scope.addAlert({type: 'success', msg: 'Successfully updated Item!'}); 
          }
        });
      }
    });
  };

```