2015-09-04 38 views
1

我創建了一個查詢,它從數據庫中提取我想要的值,並將它們放入一個數組中(var_dump顯示了這個),但是沒有創建文件。這已經從一大堆資源拼湊起來,以實現這一目標。在某處,我錯過了一些實際創建CSV文件並強制下載的地方。從Wordpress輸出查詢到CSV

function wpcp_export_inventory_feed() 
{ 


$args = array(
    'showposts'  => -1, 
'offset'   => 0, 
'orderby'   => 'date', 
'order'   => 'DESC', 
'post_type'  => 'inventory', 
'post_status'  => 'publish', 
'suppress_filters' => true 
); 

$posts = get_posts($args); 

$fh = fopen('wp-content/uploads/export.csv', 'w'); 

$vehicles[] = array(); 
$vehicles[] = array('Stock', 'Year', 'Make', 'Model', 'Trim', 'Auto Title', 'VIN', 'Price', 'Discount', 'MSRP', 'Mileage', 'Body Style', 'Engine', 'Transmission', 'Exterior Color', 'Interior Color', 'Fuel', 'MPG City', 'MPG Hwy'); 

while (have_posts()) : the_post(); 

    $autoID = get_the_ID(); 
    $year = get_post_meta($autoID, '_auto_year', true); 
    $make = get_post_meta($autoID, '_auto_make', true); 
    $model = get_post_meta($autoID, '_auto_model', true); 
    $trim = get_post_meta($autoID, '_auto_trim', true); 
    $autotitle = "$year $make $model $trim" ; 
    $autostock = get_post_meta($autoID, '_auto_stock', true); 
    $autovin = get_post_meta($autoID, '_auto_vin', true); 
    $autoprice = get_post_meta($autoID, '_auto_price', true); 
    $autodiscount = get_post_meta($autoID, '_auto_discount', true); 
    $automsrp = get_post_meta($autoID, '_auto_msrp', true); 
    $automileage = get_post_meta($autoID, '_auto_mileage', true); 
    $autobodystyle = get_post_meta($autoID, '_auto_body', true); 
    $autoengine = get_post_meta($autoID, '_auto_engine', true); 
    $autotrans = get_post_meta($autoID, '_auto_trans', true); 
    $autocolorext = get_post_meta($autoID, '_auto_color_ext', true); 
    $autocolorint = get_post_meta($autoID, '_auto_color_int', true); 
    $autofuel = get_post_meta($autoID, '_auto_fuel', true); 
    $autompgcity = get_post_meta($autoID, '_auto_gasmiles_city', true); 
    $autompghwy = get_post_meta($autoID, '_auto_gasmiles_hwy', true); 

    $vehicles[] = array($autostock, $year, $make, $model, $trim, $autotitle, $autovin, $autoprice, $autodiscount, $automsrp, $automileage, $autobodystyle, $autoengine, $autotrans, $autocolorext, $autocolorint, $autofuel, $autompgcity, $autompghwy); 

endwhile; 

foreach($vehicles AS $vehicle) { fputcsv($fh, $vehicle, ';'); } 
fclose($fh); 

var_dump($vehicles); 

} 

回答

0

我認爲你有一個路徑問題。至少你會有一個零大小的文件,如果你的邏輯的其餘部分有問題。如果你掃描所有的文件夾,你可能會找到它。

你需要給路徑根錨:

$fh = fopen ('/wp-content/uploads/export.csv', 'w');