如何在WordPress中使用PHP从数据库值创建可下载的CSV文件?

ckocjqey  于 12个月前  发布在  WordPress
关注(0)|答案(1)|浏览(90)

我试图在WordPress管理侧边栏中创建一个名为“CSV”的新页面。当你点击这个页面,我想它下载一个CSV文件填充值从数据库表。我已经为此创建了一些代码,但是当CSV文件下载时,它包含各种垃圾HTML。CSV文件基本上包含来自Admin面板的所有HTML和样式表链接。我只想要一个包含数据库值的CSV文件,所以我不确定我在这里做错了什么。
下面是我的代码:

add_action( 'admin_menu', 'my_admin_menu' );

 function my_admin_menu() {  // add a menu option to the WordPress Admin sidebar
     add_menu_page('CSV', 'CSV File', 'manage_options', 'csv-file', 'csvFile',    'dashicons-admin-post',6);
 }

 function csvFile() {
     ob_start();

     global $wpdb;

     // Set the content type to CSV
     header('Content-Type: text/csv; charset=utf-8');

    // Set the response header to specify that the file should be downloaded as an attachment
     header('Content-Disposition: attachment; filename=redirect_export.csv');

     $values=array();

     $values = array(
         ['source URL', 'target URL']
     );

     $table_name = 'wp_wbz404_redirects';

     $results = $wpdb->get_results( "SELECT * FROM $table_name"); // Query to fetch data from database table and storing in $results

     if(!empty($results)){
         foreach ($results as $result){ 
             $home = home_url();
             $source_url = $result->url;
             $source_url =  $home.$source_url;
             $id = $result->final_dest;
        
             if($id == 0){
                 continue;
             }else{
                 $target_url = get_permalink( $id );
                 //build CSV
                 $redirects = array (
                     array($source_url, $target_url)
                 );
                 $values = array_merge($values,$redirects);
              }
         }
    
         $fp = fopen('php://output', 'w');
    
         // Write the data to the file
         foreach ($values as $row) {
           fputcsv($fp, $row);
         }
    
         fclose($fp);
    
     }else{
         echo "NO RESULTS";
     }

     return ob_get_clean();
 }
olhwl3o2

olhwl3o21#

看起来你面临的问题是由于CSV文件中包含了一些HTML。为了确保CSV文件只包含数据库值,您应该阻止WordPress管理页面中的任何HTML输出。您可以在创建CSV文件之前使用'ob_clean()'函数清除所有输出缓冲区来实现此目的。
下面是代码的更新版本:

function generate_csv() {
    ob_clean(); // Clear output buffer to prevent HTML from being included
    
    $results = your_function_to_fetch_database_values(); // Replace this with your database query
    
    if (!empty($results)) {
        $values = array();
        
        foreach ($results as $result) {
            // Your code to populate $values with database values here
        }
        
        header('Content-Type: text/csv');
        header('Content-Disposition: attachment; filename="data.csv"');
        
        $fp = fopen('php://output', 'w');
        
        // Write the data to the file
        foreach ($values as $row) {
            fputcsv($fp, $row);
        }
        
        fclose($fp);
        exit();
    } else {
        echo "NO RESULTS";
    }
}

// Add your admin menu page hook here
add_action('admin_menu', 'add_csv_page');

function add_csv_page() {
    add_menu_page('CSV', 'CSV', 'manage_options', 'csv-page', 'generate_csv');
}

当您单击WordPress管理侧边栏中的“CSV”页面时,此代码应创建一个可下载的CSV文件,其中仅包含数据库值。确保将'your_function_to_fetch_database_values()'替换为实际的数据库查询,以获取所需的值。

相关问题