Jump to content

Recommended Posts

Team

   I tried to create csv output ( every value in double quotes) in my php using below function

if($generate_csv){
    generateCSV($custom[0]['details'], date('Y_m_d_')."check".".CSV", $detail_headers);
}

function generateCSV($data, $filename) {
  $file = fopen("/local/myloc/populated/".$filename,"w+");
  $i = 0;
  foreach ($data as $line) {
    $fields = array();
    foreach ($line as $k => $v)
    {
        $fields[$k] = $v;
    }
    if( $i === 0 )
    {
     fputcsv($file, array_keys($fields) ); // First write the headers
    }
    fputcsv($file, array_values($fields));
    $i++;
  }
  fclose($file);
}

I got below output with one issue (There is a blank at the end of each value)

Date,time,Device,lable,throughput_read,throughput_write
"11/19/2020 ","00:00 ","audigysg36s6101 ","host17 ","0,00 ","0,00 "
"11/19/2020 ","00:00 ","audigysg36s6101 ","host1 ","0,00 ","0,00 "
"11/19/2020 ","00:00 ","audigysg36s6101 ","host18 ","0,00 ","0,00 "

 

Could you please help me to resolve this issue

Link to comment
https://forums.phpfreaks.com/topic/311752-csv-output-using-php/
Share on other sites

First please use the code icon (<>) for your code and specify PHP. As for your specific problem, I don't know how those variables are generated (that is where the space is coming from) but simply using rtrim should solve the problem.

$fields[$k] = rtrim($v);

 

 

Thank you very much gw1500se . I somewhat messed up the question. Actually i want to create a file on CSV format.

Below is my logic to achieve my goal 

if($generate_csv){
    generateCSV($custom[0]['details'], date('Y_m_d_')."check".".CSV", $detail_headers);
}

function generateCSV($data, $filename) {
  $file = fopen("/local/myloc/populated/".$filename,"w+");
  $i = 0;
  foreach ($data as $line) {
    $fields = array();
    foreach ($line as $k => $v)
    {
        $fields[$k] = $v;
    }
    if( $i === 0 )
    {
     fputcsv($file, array_keys($fields) ); // First write the headers
    }
    fputcsv($file, array_values($fields));
    $i++;
  }
  fclose($file);
}

There is no issue in my output

Date,time,Device,lable,throughput_read,throughput_write
11/21/2020,00:00,audigysg36s6101,192905199.0,1742118968.0,11.073020990149,97.584
11/21/2020,00:05,audigysg36s6101,192914200.0,1742253920.0,11.072679922568,97.559

 

But i need to show every value in double quotes like

"11/21/2020,00:00","audigysg36s6101","192905199.0","1742118968.0","11.073020990149","97.584"

 

to get above output, i changed my code 

$fields[$k] = $v. ' ';

Now i got one blank space in at the end of each value like

"11/21/2020,00:00 ","audigysg36s6101 ","192905199.0 ","1742118968.0 ","11.073020990149 ","97.584 "

 

If i use trim then it removes all double quotes in the line that i don't need.

11/21/2020,00:00,audigysg36s6101,192905199.0,1742118968.0,11.073020990149,97.584

 

Input values to get csv

[1] => Array
        (
            [Date] => 11/21/2020
            [time] => 00:00
            [Device] => audigysg36s6101
            [lable] => 192905199.0
            [throughput_read] => 1742118968.0
            [throughput_write] => 11.073020990149
        )

    [1] => Array
        (
            [Date] => 11/21/2020
            [time] => 00:15
            [Device] => audigysg36s6101
            [lable] => 192914200.0
            [throughput_read] => 1742253920.0
            [throughput_write] => 11.072679922568

)

 

 

 

Edited by Barand
code tags added

I don't think fputcsv allows you to force quotes around values.  Generally speaking quotes should not be necessary except under specific circumstances.  Is there any particular reason you need to have every value quoted?

If you do need every value quoted, you'll  have to generate the output yourself.

To expand on what @kicken said, numeric values do not get quoted and string values only get quoted if they contain one or more commas.

If you really do need everything quoted then you could...

function generateCSV($data, $filename)
{
    $fp = fopen($filename, 'w');
    foreach ($data as $k => $v) {
        if ($k==0)  {
            fputcsv($fp, array_keys($v));
        }
        fprintf($fp, '"%s","%s","%s","%s","%s","%s"'."\n", ...array_values($v));
    }
    fclose($fp);
}

OUTPUT:

Date,time,Device,label,throughput_read,throughput_write
"11/21/2020","00:00","audigysg36s6101","192905199","1742118968","11.073020990149"
"11/21/2020","00:15","audigysg36s6101","192914200","1742253920","11.072679922568"

 

Edited by Barand

Thanks gw1500se/Barand

@gw1500se

$fields[$k] = "\"".$v."\"";  It doesn't provide desired result

Date,time,Device,label,throughput_read,throughput_write
"""11/21/2020""","""00:00""","""audigysg36s6101""","""192905199.0""","""1742118968.0""","""11.073020990149""","""97.584"""
"""11/21/2020""","""00:05""","""audigysg36s6101""","""192914200.0""","""1742253920.0""","""11.072679922568""","""97.559"""

 

@Barand

I tried to apply your logic, but no luck . I am not sure why it failed to me

function generateCSV($data, $filename)
 {
  $fp  = = fopen("/local/myloc/populated/".$filename,"w+");
     foreach ($data as $k => $v)
   {
    if( $k === 0 )
    {
      fputcsv($fp, array_keys($v)); // First write the headers
    }
    fprintf($fp, '"%s","%s","%s","%s","%s","%s"'."\n",array_keys($v));
    }
    fclose($fp);
  }

head -20 -v 2020_11_22_SERVER.CSV
==> 2020_11_22_SERVER.CSV <==
Date,time,Device,label,throughput_read,throughput_write

No Rows

 

      

 

 

Edited by Barand

@Barand

I am little bit curious that you shown the desired result. But when I used your logic in my code. It failed 

 

function generateCSV($data, $filename)
{
  $fp  = fopen("/local/myloc/populated/".$filename,"w+");
  foreach ($data as $k => $v) {
        if ($k==0)  {
            fputcsv($fp, array_keys($v));
        }
        fprintf($fp, '"%s","%s","%s","%s","%s","%s"'."\n",array_values($v));
    }
    fclose($fp);
}

 

I made only 2 changes. 1) path .. i don't think any issue

2) removed 3 dots before array_vales. If i keep that one, my report fails to run.

 

Overall my report runs without any error. Logs generated. But CSV failed to generate

 

 

The three dots are significant, expanding the array into a comma separated list of values. They won't work with a PHP version before v5.6, but if that's case then you have more work to do bringing your PHP up to date (v7.4).

Have you turned your error reporting and display on yet?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.