Javascript Wordpress和CSV文件。它能工作吗?

Javascript Wordpress和CSV文件。它能工作吗?,javascript,php,html,wordpress,csv,Javascript,Php,Html,Wordpress,Csv,我有个小问题。在我基于wordpress的网站上,我想创建一个订单状态表,人们可以使用代码检查订单的进度。它不是在线商店,所以我不使用woocommerce。包含订单进度的文件是CSV文件。 我试图通过函数使用它,但没有成功。我甚至尝试了Javascript,但我的代码在服务器上找不到该文件:( 我的问题是:我应该使用什么语言和技术来满足我的需求 非常感谢,伙计。我在之前的一个项目中实际使用了它,它实现了您所追求的目标,尽管我发现它已经被弃用了。所以我会使用它 这两个库基本上可以做的(除其他许多

我有个小问题。在我基于wordpress的网站上,我想创建一个订单状态表,人们可以使用代码检查订单的进度。它不是在线商店,所以我不使用woocommerce。包含订单进度的文件是CSV文件。 我试图通过函数使用它,但没有成功。我甚至尝试了Javascript,但我的代码在服务器上找不到该文件:( 我的问题是:我应该使用什么语言和技术来满足我的需求

非常感谢,伙计。

我在之前的一个项目中实际使用了它,它实现了您所追求的目标,尽管我发现它已经被弃用了。所以我会使用它

这两个库基本上可以做的(除其他许多事情外)是解析电子表格并根据您的请求返回相关信息


因此,您可以将电子表格放在一个单独的目录中,然后使用PHPSReadSheet库从中提取信息,并以您认为合适的任何方式呈现给客户。

我认为这就是您需要的,而不需要任何库:

首先创建一个表单(表单操作可以是您的主页,因为我们将监听
init
上的$\u GET参数,该参数在每次页面加载时运行):

现在,您可以启动生成csv报告的功能。此功能取决于您获取数据的方式,但您可以按照以下示例设置标题并输出报告:

function export_order_status_csv( $code ) {

    // Make a DateTime object and get a time stamp for the filename
    $date = new DateTime();
    $ts = $date->format( "Y-m-d-G-i-s" );

    // A name with a time stamp, to avoid duplicate filenames
    $filename = "order-statuses-export-$ts.csv";

    // Tells the browser to expect a CSV file and bring up the
    // save dialog in the browser
    header( 'Pragma: public' );
    header( 'Expires: 0' );
    header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
    header( 'Content-Description: File Transfer' );
    header( 'Content-Type: text/csv' );
    header( 'Content-Disposition: attachment;filename=' . $filename );
    header( 'Content-Transfer-Encoding: binary' );

    // This opens up the output buffer as a "file"
    $fp = fopen( 'php://output', 'w' );

    //This needs to be customised from your end, I am doing a WP_Query for this example
    $results = new WP_Query();  
    if ( $results->have_posts() ) {

        //This is set to avoid issues with special characters
        $bom = ( chr( 0xEF ) . chr( 0xBB ) . chr( 0xBF ) );

        //add BOM to fix UTF-8 in Excel
        fputs( $fp, $bom );

        // Set the headers of the csv
        fputcsv( $fp, [
            'orderID',
            'orderDate',
            'orderTotal'
        ] );

        while ( $results->have_posts() ) {
            $results->the_post();

            //Here we are inserting the row data per result fetched
            fputcsv(
                $fp,
                [
                    get_the_ID(),
                    get_the_date(),
                    'your_custom_data'
                ]
            );

        }
        wp_reset_query();
        // Close the output buffer (Like you would a file)
        fclose( $fp );
    } else {
        fputcsv( $fp, [ 'No Results' ] );
        fclose( $fp );
    }

}
导出csv时,您必须连接到在输出任何内容之前已处理的操作。在创建csv文件之前不能有任何输出,因为我们正在更新标题。

如果要从csv文件读取数据并对其进行操作以自定义csv导出,则可以使用此功能,而不是上面示例中的WP_查询:

$row=1;
if($handle=fopen(“test.csv”、“r”)!==FALSE){
while(($data=fgetcsv($handle,1000,“,”)!==FALSE){
$num=计数($data);
echo“$num字段位于第$row行:

\n”; $row++; 对于($c=0;$c<$num;$c++){ echo$data[$c]。“
\n”; } } fclose($handle); }
您可能需要在此处添加一些代码,以便开始工作。一个快速的初步建议是检查这一点非常有用,并且您有正确的想法。我可能没有很好地解释我的问题。CSV文件不需要重新打印、替换或保存。我只需要整理代码(由用户提供)使用CSV文件并获取此特定行的数据。为了从CSV读取数据,请使用答案的最后一部分,并使用从CSV获取的数据检查代码。
function check_for_export() {
    if ( isset( $_GET['csv_export'], $_GET['code'] ) && $_GET['csv_export'] == 'order_status' ) {
            ob_end_clean();
            export_order_status_csv( $_GET['code'] );
            exit();
        }
    }
}
add_action('init', 'check_for_export');
function export_order_status_csv( $code ) {

    // Make a DateTime object and get a time stamp for the filename
    $date = new DateTime();
    $ts = $date->format( "Y-m-d-G-i-s" );

    // A name with a time stamp, to avoid duplicate filenames
    $filename = "order-statuses-export-$ts.csv";

    // Tells the browser to expect a CSV file and bring up the
    // save dialog in the browser
    header( 'Pragma: public' );
    header( 'Expires: 0' );
    header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
    header( 'Content-Description: File Transfer' );
    header( 'Content-Type: text/csv' );
    header( 'Content-Disposition: attachment;filename=' . $filename );
    header( 'Content-Transfer-Encoding: binary' );

    // This opens up the output buffer as a "file"
    $fp = fopen( 'php://output', 'w' );

    //This needs to be customised from your end, I am doing a WP_Query for this example
    $results = new WP_Query();  
    if ( $results->have_posts() ) {

        //This is set to avoid issues with special characters
        $bom = ( chr( 0xEF ) . chr( 0xBB ) . chr( 0xBF ) );

        //add BOM to fix UTF-8 in Excel
        fputs( $fp, $bom );

        // Set the headers of the csv
        fputcsv( $fp, [
            'orderID',
            'orderDate',
            'orderTotal'
        ] );

        while ( $results->have_posts() ) {
            $results->the_post();

            //Here we are inserting the row data per result fetched
            fputcsv(
                $fp,
                [
                    get_the_ID(),
                    get_the_date(),
                    'your_custom_data'
                ]
            );

        }
        wp_reset_query();
        // Close the output buffer (Like you would a file)
        fclose( $fp );
    } else {
        fputcsv( $fp, [ 'No Results' ] );
        fclose( $fp );
    }

}
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);
}