Search code examples
phpfilecsvfgetcsvfputcsv

PHP, Writing to CSV not on same line?


function writePlayerInfo($playerFName, $playerSName, $phone, $email){
    $data = array();
    array_push($data, $playerFName, $playerSName, $phone, $email);
    $handleName = fopen("data/names.csv","w");
    foreach ($data as $line)
    {
        fputcsv($handleName,explode(',',$line));
    }
    fclose($handleName);
    $datas = array();
}

if (isset($_POST['submit'])) {
    $fname = $_POST['firstname'];
    $lname = $_POST['lastname'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];
    writePlayerInfo($fname, $lname, $phone, $email);
    header('Location: createplayer.php');
}

Hi guys, thanks in advance

Im trying to write to a csv file, i have got it working, my current code displays information in the csv file like this:

Dave 
Smith
01210112345
Email@stack.com

However i would like it to write to the file like this

Dave Smith 01210112345 Email@stack.com

Then i would like it so the next time i call the function it writes on the line below, any ideas?


Solution

  • fputcsv with delimiter="\t"

    function writePlayerInfo($playerFName, $playerSName, $phone, $email){
        $data = array();
        array_push($data, $playerFName, $playerSName, $phone, $email);
        $handleName = fopen("data/names.csv","w");
        fputcsv($handleName, $data, "\t");
        fclose($handleName);
        $datas = array();
    }