为什么仅导出到一列中显示的CSV数据

发布于 2025-02-08 09:11:33 字数 7013 浏览 1 评论 0原文

在应用程序过程中,我们正在生成许多动态表。因此,我们试图将数据从那些动态生成的表列数据导出到.csv文件。

$secondsql = "SELECT * FROM ".$reslt->dname." WHERE userid=:userid AND eventid=:eventid";
    
$querysec = $dbh -> prepare($secondsql); 
$querysec-> bindParam(':userid', $usrid, PDO::PARAM_STR);  
$querysec-> bindParam(':eventid', $exporteventid, PDO::PARAM_STR);                                     
$querysec-> execute();
$ressec = $querysec -> fetchAll(PDO::FETCH_OBJ);

这就是我们获取数据并将其转换为JSON并获取键值对并传递到Fputcsv的方式 $ jsonstring = json_encode($ ressec);

        $jsonDecoded = json_decode($jsonString, true);
        $keys = array_keys(json_decode($jsonString, true));
        $csvHeader=array();
        $csvData=array();
        
        foreach($jsonDecoded as $key => $val) {
           
            if (!$val) {
            }else{
                if(!$csvHeader){
                    $csvHeader = array_keys($val);
                }
           }
        }
        
        $kcnt = count($csvHeader);
        
        $tempstring = "";
        
        foreach($jsonDecoded as $val2) {
            $arrvals = array_values($val2);
            $resultStringValues = implode(",", $arrvals);
            
            $temparray=array();
            array_push($temparray,$resultStringValues);
          
            array_push($csvData,$temparray);
        }
        $tempstring = rtrim($tempstring, ",");
        $delimiter = ","; 
        $filename = $reslt->eventname. date('Y-m-d') . ".csv"; 
        $f = fopen('php://memory', 'w'); 
    
        fputcsv($f, $csvHeader, $delimiter);
       
        foreach($csvData as $fields){
            fputcsv($f,$fields,$delimiter);
        }
       
        fseek($f, 0); 
        header('Content-Type: text/csv'); 
        header('Content-Disposition: attachment; filename="' . $filename . '";');
        fpassthru($f); 

的结果是 在此处输入图像描述

为什么它不会在适当的列中分配数据? 我的$ csvheader和$ csvdata阵列输出

Array
(
    [0] => id
    [1] => eventid
    [2] => userid
    [3] => fullname
    [4] => email
    [5] => mobile
    [6] => reg_date
)
1
Array
(
    [0] => Array
        (
            [0] => 21,22,19,khasim,[email protected],8786468768,2022-05-30 00:00:00
        )

    [1] => Array
        (
            [0] => 22,22,19,tanveer,[email protected],2343434,2022-05-30 01:00:00
        )

    [2] => Array
        (
            [0] => 23,22,19,tan,[email protected],24343,2022-05-30 00:00:00
        )

    [3] => Array
        (
            [0] => 24,22,19,raju,[email protected],234324324,2022-05-30 01:00:00
        )

    [4] => Array
        (
            [0] => 26,22,19,tan,[email protected],24343,2022-05-30 00:00:00
        )

    [5] => Array
        (
            [0] => 27,22,19,raju,[email protected],234324324,2022-05-30 01:00:00
        )

    [6] => Array
        (
            [0] => 29,22,19,raju,[email protected],234324324,2022-05-30 01:00:00
        )

    [7] => Array
        (
            [0] => 30,22,19,raju,[email protected],234324324,2022-05-30 01:00:00
        )

    [8] => Array
        (
            [0] => 31,22,19,tan,[email protected],24343,2022-05-30 00:00:00
        )

    [9] => Array
        (
            [0] => 32,22,19,raju,[email protected],234324324,2022-05-30 01:00:00
        )

    [10] => Array
        (
            [0] => 33,22,19,raju,[email protected],234324324,2022-05-30 01:00:00
        )

    [11] => Array
        (
            [0] => 34,22,19,ttttt,[email protected],234324324,2022-05-30 01:00:00
        )

    [12] => Array
        (
            [0] => 35,22,19,mohan,[email protected],123456789,2022-05-30 01:00:00
        )

    [13] => Array
        (
            [0] => 36,22,19,seet,[email protected],123456789,2022-05-30 01:00:00
        )

    [14] => Array
        (
            [0] => 37,22,19,bhadrachalam,[email protected],865956854,2022-06-15 07:17:49
        )

)

我弄乱了数组并完成了所有不必要的转换,因为没有人指着我做错了什么,我设法解决了解决方案,因此将解决方案粘贴到了其他

$secondsql = "SELECT * FROM ".$reslt->dname." WHERE userid=:userid AND eventid=:eventid";
    
    $querysec = $dbh -> prepare($secondsql); 
    $querysec-> bindParam(':userid', $usrid, PDO::PARAM_STR);  
    $querysec-> bindParam(':eventid', $exporteventid, PDO::PARAM_STR);                                     
    $querysec-> execute();
    //$ressec = $querysec -> fetchAll(PDO::FETCH_OBJ);
    $table_fields = array_keys($querysec->fetch(PDO::FETCH_ASSOC));
    $ressec = $querysec -> fetchAll(PDO::FETCH_ASSOC);
    
    
        $delimiter = ","; 
            $filename = $reslt->eventname. date('Y-m-d') . ".csv"; 
            $f = fopen('php://memory', 'w'); 
    
        fputcsv($f, $table_fields, $delimiter);
       
        foreach($ressec as $fields){
                        
            fputcsv($f,$fields,$delimiter);
             
        }
        fseek($f, 0); 
        header('Content-Type: text/csv'); 
        header('Content-Disposition: attachment; filename="' . $filename . '";'); 
        fpassthru($f);

We have many dynamic tables being generated in the process of our application. so we are trying to export the data from those dynamically generated table column data to .csv file.

$secondsql = "SELECT * FROM ".$reslt->dname." WHERE userid=:userid AND eventid=:eventid";
    
$querysec = $dbh -> prepare($secondsql); 
$querysec-> bindParam(':userid', $usrid, PDO::PARAM_STR);  
$querysec-> bindParam(':eventid', $exporteventid, PDO::PARAM_STR);                                     
$querysec-> execute();
$ressec = $querysec -> fetchAll(PDO::FETCH_OBJ);

This is how we are fetching the data and converting it to json and getting key value pairs and passing onto fputcsv
$jsonString = json_encode($ressec);

        $jsonDecoded = json_decode($jsonString, true);
        $keys = array_keys(json_decode($jsonString, true));
        $csvHeader=array();
        $csvData=array();
        
        foreach($jsonDecoded as $key => $val) {
           
            if (!$val) {
            }else{
                if(!$csvHeader){
                    $csvHeader = array_keys($val);
                }
           }
        }
        
        $kcnt = count($csvHeader);
        
        $tempstring = "";
        
        foreach($jsonDecoded as $val2) {
            $arrvals = array_values($val2);
            $resultStringValues = implode(",", $arrvals);
            
            $temparray=array();
            array_push($temparray,$resultStringValues);
          
            array_push($csvData,$temparray);
        }
        $tempstring = rtrim($tempstring, ",");
        $delimiter = ","; 
        $filename = $reslt->eventname. date('Y-m-d') . ".csv"; 
        $f = fopen('php://memory', 'w'); 
    
        fputcsv($f, $csvHeader, $delimiter);
       
        foreach($csvData as $fields){
            fputcsv($f,$fields,$delimiter);
        }
       
        fseek($f, 0); 
        header('Content-Type: text/csv'); 
        header('Content-Disposition: attachment; filename="' . $filename . '";');
        fpassthru($f); 

The result for this is
enter image description here

why it is not splitting the data in the appropriate columns?
my $csvHeader and $csvData array outputs

Array
(
    [0] => id
    [1] => eventid
    [2] => userid
    [3] => fullname
    [4] => email
    [5] => mobile
    [6] => reg_date
)
1
Array
(
    [0] => Array
        (
            [0] => 21,22,19,khasim,[email protected],8786468768,2022-05-30 00:00:00
        )

    [1] => Array
        (
            [0] => 22,22,19,tanveer,[email protected],2343434,2022-05-30 01:00:00
        )

    [2] => Array
        (
            [0] => 23,22,19,tan,[email protected],24343,2022-05-30 00:00:00
        )

    [3] => Array
        (
            [0] => 24,22,19,raju,[email protected],234324324,2022-05-30 01:00:00
        )

    [4] => Array
        (
            [0] => 26,22,19,tan,[email protected],24343,2022-05-30 00:00:00
        )

    [5] => Array
        (
            [0] => 27,22,19,raju,[email protected],234324324,2022-05-30 01:00:00
        )

    [6] => Array
        (
            [0] => 29,22,19,raju,[email protected],234324324,2022-05-30 01:00:00
        )

    [7] => Array
        (
            [0] => 30,22,19,raju,[email protected],234324324,2022-05-30 01:00:00
        )

    [8] => Array
        (
            [0] => 31,22,19,tan,[email protected],24343,2022-05-30 00:00:00
        )

    [9] => Array
        (
            [0] => 32,22,19,raju,[email protected],234324324,2022-05-30 01:00:00
        )

    [10] => Array
        (
            [0] => 33,22,19,raju,[email protected],234324324,2022-05-30 01:00:00
        )

    [11] => Array
        (
            [0] => 34,22,19,ttttt,[email protected],234324324,2022-05-30 01:00:00
        )

    [12] => Array
        (
            [0] => 35,22,19,mohan,[email protected],123456789,2022-05-30 01:00:00
        )

    [13] => Array
        (
            [0] => 36,22,19,seet,[email protected],123456789,2022-05-30 01:00:00
        )

    [14] => Array
        (
            [0] => 37,22,19,bhadrachalam,[email protected],865956854,2022-06-15 07:17:49
        )

)

I messed up the array and done all unnecessary conversions, as no one pointed at what I did wrong I managed to solve so pasting the solution for others

$secondsql = "SELECT * FROM ".$reslt->dname." WHERE userid=:userid AND eventid=:eventid";
    
    $querysec = $dbh -> prepare($secondsql); 
    $querysec-> bindParam(':userid', $usrid, PDO::PARAM_STR);  
    $querysec-> bindParam(':eventid', $exporteventid, PDO::PARAM_STR);                                     
    $querysec-> execute();
    //$ressec = $querysec -> fetchAll(PDO::FETCH_OBJ);
    $table_fields = array_keys($querysec->fetch(PDO::FETCH_ASSOC));
    $ressec = $querysec -> fetchAll(PDO::FETCH_ASSOC);
    
    
        $delimiter = ","; 
            $filename = $reslt->eventname. date('Y-m-d') . ".csv"; 
            $f = fopen('php://memory', 'w'); 
    
        fputcsv($f, $table_fields, $delimiter);
       
        foreach($ressec as $fields){
                        
            fputcsv($f,$fields,$delimiter);
             
        }
        fseek($f, 0); 
        header('Content-Type: text/csv'); 
        header('Content-Disposition: attachment; filename="' . $filename . '";'); 
        fpassthru($f);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戏剧牡丹亭 2025-02-15 09:11:33

尝试从“,”到“”替换数据分离器,因为您使用Microsoft Excel。或在libre Office计算中打开同一文件。

Try replace data seperator from "," to ";", because You using Microsoft excel. Or open same file in libre office calc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文