0

I have to export all my products from database into a csv file when executing the following script but i can't manage to put together the function that returns all my products from database into an array or iterator. My php code is put below. What am i doing wrong? Thank you.

<?php
require_once("config/db-connect.php");
function toate_produsele_active() {

$array_produse = mysqli_query($mysqli, "SELECT product_id, product_name, category_url, product_short_desc, product_price, product_photo FROM mb95_products");

while ($row = mysqli_fetch_assoc($array_produse)) {
print_r($row);
}
}

$f = fopen('php://output', 'wb');
if($f) {
foreach(toate_produsele_active() as $produs) {
    $coloane = array(
        $produs['product_id'],
        $produs['product_name'],
        $produs['category_url'],
        $produs['product_short_desc'],
        $produs['product_price'],
        implode('[,]', str_replace('[,]', '[%2C]', $produs['product_photo'])),
    );
    fputcsv($f, $coloane, ';', '"');
}
fclose($f);
}
?>

My final desired result should look something like this:

1;Titlu produs1;categorie;Descriere produs1;RON;60;5;product_photo.jpg

adrianadr
  • 17
  • 1
  • 1
    your toate_produsele_active() does not return anything. and your foreach function expects an array. Use the 2nd answer from this question http://stackoverflow.com/questions/125113/php-code-to-convert-a-mysql-query-to-csv – Dimi Mar 23 '17 at 18:59
  • I need to upload my products into another website so they gave me the secound part of the code. I don;t need a csv file, only to display the prodcuts array in the same page where the script is executed. So how the toate_produsele_active() function should work for the provided script :(? – adrianadr Mar 23 '17 at 19:08

0 Answers0