doudeng1870 2015-12-08 05:53
浏览 48

WP_List_Table将数据导出为CSV格式

I am using WP_List_Table class to make a table at back end which fetches records from myCustomTable. Is there any feature/plugin available to export the single & multiple rows to CSV format. I have tried so many plugin but they all come up with the whole table export.

Thanks

  • 写回答

1条回答 默认 最新

  • duandou2763 2015-12-08 06:39
    关注

    yes you can do. this an an example of my code where i use to get the DB table to excel formate. you just replace the DB connection, table,table fields, and query as required this code may helps you

    <?php
    include '../common/inc.common.php';//db connectivity 
    
    $class=$_REQUEST['class_id'];//get the class id
    $line .= "
    ";
    $filename='../app/class_export_student_hsc.csv';// file name
    $fp = fopen($filename, "w");
    
    $sql="SELECT t1 . * ,y.aca_year
    FROM (
    SELECT  name,fathername,school_name,edu_disctrict,revenue_disctrict,nationality,religion,caste,castetype,gender,dob,dobword,mark1,stat
    FROM **myCustomTable**
    )t1, academic_years y
    WHERE t1.academic_year=y.refid
    AND t1.class =$class
    ";
    
    
    $studentArray=$Cobj->run_query($sql);
    $line = "";
    foreach($studentArray as $name => $valuee) {
    $comma = "";
        foreach($valuee as $key2 => $value){
        $line .= $comma . '"' . str_replace('"', '""', $key2) . '"';
        $comma = ",";
    }
        $line .= "
    ";
        break;
        }
    foreach($studentArray as $name => $valuee) {
    $comma = "";
        foreach($valuee as $key2 => $value){
        $line .= $comma . '"' . str_replace('"', '""', $value) . '"';
        $comma = ",";
        }   
        $line .= "
    ";
        }
    fputs($fp, $line);
    fclose($fp);
    if (file_exists($filename)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="'.basename($filename).'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($filename));
        readfile($filename);
        exit;
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用