1

i created a small script that i generating a csv file with a content from a mysql db. Everything is working fine but i am struggling with one issue... as you can see i am generating a file for a WooCommerce product import. My database contain's only some of items needed for the csv so i need to generate the other items automatically. So in example i like to add a row 'type' and add a value "simple" or 'in_stock" with a value '1' to all of the items. I am learning php and i am not sure how to add it to the while loop.

if (isset($_POST["export"])) {
    $connect = mysqli_connect("localhost", "root", "root", "dh_products");
    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename=data.csv');
    $output = fopen("php://output", "w");

    fputcsv($output, array(
       'id',
       'type',
       'SKU',
       'Name',
       'Published',
       'Is featured?',
       'Visibility in catalog',
       'Short description',
       'Description',
       'dh_symmetry',
       'Date sale price starts',
       'Date sale price ends',
       'Tax status',
       'Tax class',
       'In stock?',
       'Stock',
       'Low stock amount',
       'Backorders allowed?',
       'Sold individually?',
       'Weight (kg)',
       'Length (cm)',
       'Width (cm)',
       'Height (cm)',
       'Allow customer reviews?',
       'Purchase note',
       'Sale price',
       'Regular price',
       'Categories',
       'Tags',
       'Shipping class',
       'Images',
       'Download limit',
       'Download expiry days',
       'Parent',
       'Grouped products',
       'Upsells',
       'Cross-sells',
       'External URL',
       'Button text',
       'Position',
      ));

    $query = "SELECT * from dh ORDER BY id DESC";

    $result = mysqli_query($connect, $query);

    while ($row = mysqli_fetch_assoc($result)) {
        fputcsv($output, $row);
    }

    fclose($output);
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Wojciech
  • 103
  • 1
  • 3
  • 12

1 Answers1

1

What kind of column do you need.

Add simply

,' ' as testcolumn 

to your select statement.

And before you make your

fputcsv($output, $row);

you change the value of that new cloumn

$row["testcolumn"} = xyz;
fputcsv($output, $row);

Of Course you also need to change:

fputcsv($output, array(

If you want the new Column at a specific place, you can't use

Select * anymore

For Static text, only to complete the answer, you simply add

,'some static text' as testcolumn 
nbk
  • 45,398
  • 8
  • 30
  • 47
  • i think i confuse you a little, i like to add a column to csv, so imagine i have 10k products and i like to add for each of the product "type" = "simple" because i dont have this on my sql table. – Wojciech Oct 09 '19 at 07:37
  • Lok you asked how can i add a column / row. I explained how you add a column. In this new column you cann write anything even a type. Of course you have to change **$row["testcolumn"} = xyz;** with yopuir own logic, I don't know what you want to add and how to obtain this Information. This is soemthing you have to figure out, becauswe noboy knows what you want to do. – nbk Oct 09 '19 at 09:37