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

1 Answers1

0

let's say that toate_produsele_active() it's working as expected. I have created three functions, so the code can be reused and i have added the comments in the code:

<?php
// Create the header for the csv file (ID, Titlu, Descriere, Pret)
function getHeader()
{
    $csv = '';
    $headerData = array();
    $header = 'ID, Titlu, Descriere, Pret';
    $columns = explode(',', $header);
    foreach ($columns as $column) {
        $headerData[] = $column;
    }
    // the titles are separated by the semicolon
    $csv .= implode(';', $headerData) . "\n";

    return $csv;
}
// add the products data into an array and then fill the csv file 
// with rows. Separated by semicolon
function iterateItems($csv)
{    
    $data = [];    
    foreach (toate_produsele_active() as $produs) {
        $data[] = $produs['product_id'];
        $data[] = $produs['product_name'];
        $data[] = $produs['category_url'];
        $data[] = $produs['product_short_desc'];

        $csv .= implode(';', $data) . "\n";
    }

    return $csv;
}

function exportAll()
{
    // name and location of the file
    $csvFile = 'products.csv';

    // create the header: ID, Titlu, Descriere, Pret
    $csvHeader = getHeader();

    // add the header and then the product data into the csv file
    $csv = iterateItems($csvHeader);

    // save he file (location, file)
    file_put_contents($csvFile, $csv);
}
// call the function
exportAll();
AcidBurn
  • 21
  • 7
  • Hi! Please refrain from code only answers, and instead explain why you changed what in the code alongside it. – LoahL Oct 23 '22 at 17:31