WordPress和CSV文件.能行吗? [英] Wordpress and CSV file. Can it work ?

查看:54
本文介绍了WordPress和CSV文件.能行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题.在我基于Wordpress的网站上,我想创建一个Order-Status表单,人们可以在其中使用代码并检查其订单进度.它不是在线商店,所以我不使用woocommerce.包含订单进度的文件是CSV文件.我试图通过一个函数来使用它,但是没有用.我什至尝试使用Javascript,但我的代码无法在服务器上找到文件:(我的问题是:我应该使用哪种语言和哪种技术.

I have a small problem. On my wordpress based website I want to create an Order-Status form where people can use a code and check their order's progress. It's not an online store so I don't use woocommerce. The file containing the order's progress is a CSV file. I tried to use that through a function but didn't work. I even tried Javascript but my code can't find the file on server :( My question is: What language and what technique should I use for my need.

非常感谢.

推荐答案

我认为这是您所需要的,而无需任何库:

I think this is what you are looking for without the need of any libraries:

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

First You create a form (the form action can be your homepage since we will be listening for the $_GET parameters on init, which is run on every page load):

<form action="<?php echo site_url() ?>">
    <input type="hidden" name="csv_export" value="order_status" />
    <input type="text" name="code" />
    <input type="submit" value="Download Report" name="download_report" />
</form>

比起您需要在functions.php中的 init 上添加一个操作来侦听get参数 csv_export 来启动您的功能来准备csv文件和输出可供下载:(在创建csv之后,我们将使用exit();函数来确保在此过程之后没有其他操作.)

Than you need to add an action on init in functions.php to listen for the get parameter csv_export in order to start your functionality to prepare the csv file and output it for download: (we are using the exit(); function after we create csv to make sure that nothing else runs after this process.)

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');

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

Now you can start the functionality to generate the csv report. This function depends on how you are fetching the data but you can follow this example to set the headers and output the report:

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_Query:

If you want to read the data from a csv file and manipulate it to customise your csv export you can use this function instead of the WP_Query in example above: http://php.net/manual/en/function.fgetcsv.php

$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);
}

这篇关于WordPress和CSV文件.能行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆