Jump to content

Writing array to ZIP file


smidgen11

Recommended Posts

HI,

 

I have some code that saves a zip file to a directory that contains a csv file. The code I currently have, successfully creates a zip file containing the csv file I want to create. The issue is, the only record that gets written to the csv file inside the ZIP is the last record of the array. I know this is probably something very simple but I am having some trouble with it. Ive been google'ing but to no avail. Here is my code:

 


<?php include("sqlconnect.php");?>	
<?php
  $sql="select Username as 'ALIAS', Extension as 'DTMF_ACCESS_ID' from $tbl_entering";
  $result=mysql_query($sql);

  $filename = 'entering.VMAIL';
  $filename = $filename."_".date("Y-m-d_H-i",time());
  $header="ALIAS,DTMF_ACCESS_ID\n";

    while($rows=mysql_fetch_array($result))
  {					 
        $data="$rows[0],$rows[1]\n";
  }



  $csvdata= $header.$data;
  $zipname = 'Batch_'.date("Y-m-d_H",time());

  $zip = new ZipArchive;
  $res = $zip->open("./batch/$zipname.zip", ZipArchive::CREATE);
  if ($res === TRUE) {
    $zip->addFromString("$filename.csv", $csvdata);
    $zip->close();
    echo 'ok';
      } else {
        echo 'failed';
      }
?>

 

Thanks for the help.

Link to comment
Share on other sites

You overwrite $data each time through the loop so on the last iteration it contains only the last row.  Try this:

 

$data .= "$rows[0],$rows[1]\n";

 

And you probably need this before the loop so that you don't generate a notice the first time through:

 

$data = "";

 

Or shorter:

 

$csvdata = "ALIAS,DTMF_ACCESS_ID\n";

    while($rows=mysql_fetch_array($result))
  {					 
        $csvdata .= "$rows[0],$rows[1]\n";
  }

Link to comment
Share on other sites

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.