Jump to content

Recommended Posts

Basically trying to export a list of products in an array to a comma separated csv. I;ve managed to get it working but would like to know how to make use of the array ID's. Below is what I have

 

<?php
//The list of products
$defineProducts[801] = array(name=>'Product 1', price=>'175.95');
$defineProducts[802] = array(name=>'Product 2', price=>'235.95');
$defineProducts[803] = array(name=>'Product 3', price=>'245.95');
$defineProducts[804] = array(name=>'Product 4', price=>'215.95');
$defineProducts[805] = array(name=>'Product 5', price=>'230.95');

   $filename = date("Ymd")."_csvfile.csv";
   header("Content-type: application/csv");
   header("Content-Disposition: attachment; filename=$filename");
   $field_arr = array('productid','name','price','description');
   foreach($field_arr as $val)
    rtrim_csv_out($val);
   echo "\n";
   for($i=1; $i<10; $i++)
   {
    rtrim_csv_out( "$i" );
    rtrim_csv_out( $defineProducts[801]['name'] ); //currently only exporting product 801
    rtrim_csv_out( $defineProducts[801]['price'] );
    rtrim_csv_out( "description test $i" );
    echo "\n";
   }
   function rtrim_csv_out($str)
   {
    echo '"'.rtrim(str_replace('"','""',$str)).'",';
   }
?>

Link to comment
https://forums.phpfreaks.com/topic/269580-export-product-array-to-csv/
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.