Wordpress 将自定义 Table 导出到 CSV

Wordpress Export Custom Table to CSV

我有一个自定义数据库 table,我要将信息存储到其中,我需要将选定的行 (id) 导出到 csv 文件。目前我已经为我的批量操作添加了 "Export" 选项,它将整个 table 导出到 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" );
                    }
                }
            }

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

$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) . ")");