简体   繁体   中英

php to excel export data multiple rows

I need to export php data to excel, but the point is that I need export multirows as how many there is in cart. But everytime all same rows like iphone 11 goes 5 times in 1 row. 在此处输入图像描述

output should be like this

在此处输入图像描述

MY Code:

$sql = "SELECT product_name, product_sku FROM `cart` WHERE `invoice_id` = '$inv'";  
$setRec = mysqli_query($conn, $sql);  
$columnHeader = '';  
$columnHeader = "title" . "\t" . "sku" . "\t";  
$setData = '';  
  while ($rec = mysqli_fetch_row($setRec)) {  


   //print_r($rec);
    $rowData = '';  

$sql2 = "SELECT qty FROM `cart` WHERE `product_sku` = '$rec[1]'";  
$setRec2 = mysqli_query($conn, $sql2);  

$rec2 = mysqli_fetch_row($setRec2);


    
//fetch all rows
    foreach ($rec as $value) {  

  
 // multiple rows how many "qty"  

  for($i=0; $i<$rec2[0]; $i++) {

        $value = '"' . $value . '"' . "\t";  
        $rowData .= $value;  
    }}
  

    $setData .= trim($rowData) . "\n";  
}
  
header("Content-type: application/octet-stream");  
header("Content-Disposition: attachment; filename=User_Detail.xls");  
header("Pragma: no-cache");  
header("Expires: 0");  
$setData = mb_convert_encoding($setData, 'HTML-ENTITIES', 'UTF-8');
 echo ucwords($columnHeader) . "\n" . $setData . "\n"; 

 

I think I fixed it for you ( use csv, instead of xls )

You don't really want to use an xls file because that is a binary data format, as well as xlsx. CSV or comma separated value is typically the way to output report data like this. That makes it a bit more portable too for other programs.

$sql = "SELECT product_name, product_sku, qty FROM `cart` WHERE `invoice_id` = '$inv'";  
$setRec = mysqli_query($conn, $sql);

$columnHeader = ["title","sku"];

$f = fopen('php://temp', 'w+');
//add the header row
fputcsv($f, $columnHeader);

while ($rec = mysqli_fetch_row($setRec)) {  
    //loop up to the qty
    for($i=0; $i<$rec[2]; $i++) {
        //we can make a new array here that just has title and sku
        fputcsv($f, [$rec[0], $rec[1]]);
    }} 
}

header("Content-type: application/octet-stream");  
header("Content-Disposition: attachment; filename=User_Detail.csv");  
header("Pragma: no-cache");  
header("Expires: 0");  

echo stream_get_contents($f, null, 0);

I didn't really test this, but it should be pretty close.

The biggest mistake was trying to do two Queries instead of just one.

For reference, look a these PHP functions.

https://www.php.net/manual/en/function.fputcsv.php

https://www.php.net/manual/en/function.stream-get-contents.php

https://www.php.net/manual/en/wrappers.php.php

A bit to explain the code

This part, fopen('php://temp', 'w+'); just opens a temporary file (a file stream) for the csv file functions to use.

Then you just get your stuff from the DB, loop over it and put it into the temporary file using those functions.

At the end we can use stream_get_contents instead of something like file_get_contents because we don't have a file name. The third argument 0 just rewinds the file back to the beginning because we were at the end from writing to it. The reason for w+ when we opened it is so we can also read the file at the end.

Just picture a file stream like an old cassette tape.

You could also do this directly to the output buffer, but that's another topic for another day.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM