【问题标题】:Wordpress Export Custom Table to CSVWordpress 将自定义表格导出为 CSV
【发布时间】:2015-05-21 06:25:17
【问题描述】:

我有一个要存储信息的自定义数据库表,我需要将选定的行(id)导出到 csv 文件。目前,我在批量操作中添加了“导出”选项,它将整个表导出到 CSV 文件,但我只需要导出已检查批量选项的条目。这是我到目前为止的代码。

删除功能在选择多个复选框时起作用。

function column_cb($item)
    {
        return sprintf(
            '<input type="checkbox" name="id[]" value="%s" />',
            $item['id']
        );
    }    

function get_bulk_actions()
        {
            $actions = array(
                'delete' => 'Delete'
            );
            return $actions;
        }


    function process_bulk_action()
            {
                global $wpdb;
                $bp_table_name = $wpdb->prefix . 'custom_table'; // do not forget about tables prefix

        ?>
                <script type="text/javascript">

              jQuery(document).ready(function() {

                jQuery('<option>').val('export').text('<?php _e('Export')?>').appendTo("select[name='action']");

                jQuery('<option>').val('export').text('<?php _e('Export')?>').appendTo("select[name='action2']");

              });

            </script>
                <?php
                if ('export' === $this->current_action()) {
                    ob_end_clean();
                    $sql = $wpdb->get_results( "SELECT * FROM $bp_table_name");

                    if (!$sql) {
                        die('Invalid query: ' . mysql_error());
                    }

                    // Get The Field Name
                    $output .= 'Exhibitor Company'. ',';
                    $output .= 'Exhibitor Representative'. ',';
                    $output .= 'Position Title'. ',';
                    $output .= 'Mailing Address'. ',';
                    $output .= 'City'. ',';
                    $output .= 'State'. ',';
                    $output .= 'Zip'. ',';
                    $output .= 'Website'. ',';
                    $output .= 'Office Phone'. ',';
                    $output .= 'Cell Phone'. ',';
                    $output .= 'Email'. ',';
                    $output .= 'Backup Email'. ',';
                    $output .= 'Products/Services'. ',';
                    $output .= 'Same Booth Space'. ',';
                    $output .= 'Booth Numbers'. ',';
                    $output .= 'Convention Center'. ',';
                    $output .= 'Expo Halls'. ',';

                    $output .="\n";

                    // Get Records from the table

                    foreach ($sql as $row) {

                    $output .="\n";
                    $output .='"'.$row->exhibitor_company.'",';
                    $output .='"'.$row->exhibitor_rep.'",';
                    $output .='"'.$row->title.'",';
                    $output .='"'.$row->address.'",';
                    $output .='"'.$row->city.'",';
                    $output .='"'.$row->state.'",';
                    $output .='"'.$row->zip.'",';
                    $output .='"'.$row->website.'",';
                    $output .='"'.$row->office_phone.'",';
                    $output .='"'.$row->cell.'",';
                    $output .='"'.$row->email.'",';
                    $output .='"'.$row->backup_email.'",';
                    $output .='"'.$row->products_services.'",';
                    $output .='"'.$row->same_space.'",';
                    $output .='"'.$row->booth_numbers.'",';
                    $output .='"'.$row->convention_center.'",';
                    $output .='"'.$row->expo_halls.'",';

                    }
                    $output .="\n";
                    // Download the file

                    $file = "custom_table";
                    $filename = $file."_".date("Y-m-d_H-i",time());
                    header("Content-type: application/vnd.ms-excel");
                    header("Content-disposition: csv" . date("Y-m-d") . ".csv");
                    header( "Content-disposition: filename=".$filename.".csv");

                    echo $output;
                    exit;

                }

                $entry_id = ( is_array( $_REQUEST['id'] ) ) ? $_REQUEST['id'] : array( $_REQUEST['id'] );

                if ( 'delete' === $this->current_action() ) {
                    global $wpdb;

                    foreach ( $entry_id as $id ) {
                        $id = absint( $id );
                        $wpdb->query( "DELETE FROM $bp_table_name WHERE id = $id" );
                    }
                }
            }

【问题讨论】:

    标签: wordpress export-to-csv


    【解决方案1】:

    所以,我终于回答了我自己的问题。万一其他人遇到这个问题,这里就是答案。

    $idList = array();
    foreach( $entry_id as $id){
            if ((int)$id > 0) {
            $idList[] = (int)$id;
            }
    }//end foreach
    
    $sql = $wpdb->get_results( "SELECT * FROM $bp_table_name WHERE id IN(" . implode(",", $idList) . ")");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-05
      • 2012-06-08
      • 2013-08-06
      • 1970-01-01
      • 2018-06-27
      • 2013-07-25
      • 2015-07-23
      相关资源
      最近更新 更多