Wordpress usermeta导出到csv

时间:2011-09-23 15:37:46

标签: php wordpress

我正在尝试下载所有user_meta数据,并在导出后以csv中的漂亮布局表格格式显示。

我有一个问题,如何布置meta对表csv导出不好。有没有解决这个问题的方法?

3   1   suburb  Melbourne
4   1   state   VIC
5   1   postcode    3000
6   1   maternitybg Yes
7   1   occupation   
8   1   wp_user_level   0
9   1   nick_name   Lisa
10  1   last_name   Masters

所以上面是示例,但我希望meta_keys是列标题和值,以显示为每列的字段值。我该怎么做?

以下是我到目前为止......

$select = "select distinct(user_id), meta_key, meta_value from wp_usermeta order by user_id";

$export = mysql_query ( $select ) or die ( "Sql error : " . mysql_error( ) );

$fields = mysql_num_fields ( $export );

for ( $i = 0; $i < $fields; $i++ )
{
    $header .= mysql_field_name( $export , $i ) . "\t";
}

while( $row = mysql_fetch_row( $export ) )
{
    $line = '';
    foreach( $row as $value )
    {                                            
        if ( ( !isset( $value ) ) || ( $value == "" ) )
        {
            $value = "\t";
        }
        else
        {
            $value = str_replace( '"' , '""' , $value );
            $value = '"' . $value . '"' . "\t";
        }
        $line .= $value;
    }
    $data .= trim( $line ) . "\n";
}
$data = str_replace( "\r" , "" , $data );

if ( $data == "" )
{
    $data = "\n(0) Records Found!\n";                        
}

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=facilitators.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";

0 个答案:

没有答案