2013-01-15 147 views
0

如何將我的循環合併到CSV文件中導出?導出CSV文件下載文章

<?php $answers = new WP_Query(array('cat' => 25, 'order' => 'DESC')); ?> 
<table width="100%" border="1" cellspacing="1" cellpadding="1"> 
     <tr> 
     <td valign="top">Name</td> 
     <td valign="top">Submission/production title</td> 
     <td valign="top">Performance space</td> 
     </tr> 
<?php if ($answers->have_posts()) : while ($answers->have_posts()) : $answers->the_post(); ?> 
     <tr> 
     <td><?php echo the_title(); ?></td> 
     <td><?php echo the_field('submit_submission_production_title'); ?></td> 
     <td><?php echo the_field('submit_performance_space'); ?></td> 
     </tr> 
    <?php endwhile; ?> 
    </table>      
<?php endif; ?> 

我該如何與fputcsv合併?

<?php 
$list = array (
    'aaa,bbb,ccc,dddd', 
    '123,456,789', 
    '"aaa","bbb"' 
); 

$fp = fopen('file.csv', 'w'); 

foreach ($list as $line) { 
    fputcsv($fp, split(',', $line), ',', '&quot;'); 
} 
fclose($fp); 
?> 

回答

1

首先,the_titlethe_field *是自己做echo功能,你不需要echo the_title()

*假設這是來自高級自定義字段。

只是建立一個數組,如:

$answers = new WP_Query(array('cat' => 25, 'order' => 'DESC')); 
if ($answers->have_posts()) { 
    $list = array('Name,Submission/production title,Performance space'); 
    while ($answers->have_posts()) { 
     $title = get_the_title(); 
     $field1 = get_field('submit_submission_production_title'); 
     $field2 = get_field('submit_performance_space'); 
     $list[] = $title . ',' . $field1 . ',' . $field2; 

     // IF ENCLOSING DOUBLE QUOTES ARE NEEDED 
     // $list[] = '"' . $title . '","' . $field1 . '","' . $field2 . '"'; 
    } 
} 
+0

感謝這個,我給它一個去。那麼我怎麼能把它放在一個鏈接中,說「下載文件」,這樣它可以保存爲CSV? – Rob

+0

我不太清楚如何處理'fputcsv'和'fclose',但我認爲這很容易,很可能,答案是在這個龐大的存儲庫中搜索;) – brasofilo