2013-02-03 63 views
0

我寫了一個將mysql數據轉換爲csv文件的代碼。問題是,它顯示的結果水平爲由php水平生成格式化csv文件

 
DATE  AUTHOR LIKES COMMENTS` 
1/31/2013 WTF Facts 211 3 
1/31/2013 WTF Facts 211 23 
1/31/2013 WTF Facts 211 23 
1/31/2013 WTF Facts 211 23 
1/31/2013 WTF Facts 211 23 

DATE  AUTHOR LIKES COMMENTS` 
1/31/2013 WTF Facts 211 3 
1/31/2013 WTF Facts 211 23 
1/31/2013 WTF Facts 211 23 
1/31/2013 WTF Facts 211 23 
1/31/2013 WTF Facts 211 23 

但是我想它垂直的

 
DATE  AUTHOR LIKES COMMENTS` DATE AUTHOR  LIKES COMMENTS` 
1/31/2013 WTF Facts 211 3   1/31/2013 WTF Facts 211  23 
1/31/2013 WTF Facts 211 23   1/31/2013 WTF Facts 211  23 
1/31/2013 WTF Facts 211 23   1/31/2013 WTF Facts 211  3 
1/31/2013 WTF Facts 211 23   1/31/2013 WTF Facts 211  23 
1/31/2013 WTF Facts 211 23   1/31/2013 WTF Facts 211  23 

這裏是我的代碼:

 <?php 
     if(isset($_POST['submit'])): 
     set_time_limit(99999999); 
     ini_set('memory_limit', "9999M"); 
     $filename_csv = "export_".rand(1000000,9999999)."_".rand(1000000,9999999).".csv"; 
     $path = "serp_csvs_page/".$filename_csv; 
     $fp = fopen($path, 'w'); 
     $page_ids = $_POST['page_id']; 
     $date_from = $_POST['date_from']; 
     $date_to = $_POST['date_to']; 

     foreach($page_ids as $page_id): 
      $brline = array("\n"); 
      $page_name = get_page_name_by_id($page_id); 
      $dates = checkCsvQuotes("DATE: FROM {$date_from} TO {$date_to}") ; 
      $page_name = checkCsvQuotes("PAGE ID: {$page_name['page_id']}"); 
      $header = array($dates, "\n", $page_name); 
      fputcsv($fp, $header); 
      fputcsv($fp, $brline); 
      $query = sprintf("SELECT post_date AS `DATE`, post_from AS `AUTHOR`, message AS `MESSAGE`, total_likes AS `TOTAL LIKES`, total_comments AS `TOTAL COMMENTS` FROM tbl_contents_pages WHERE `pid_id` = '$page_id' AND `post_date` >= '$date_from' AND `post_date` <= '$date_to'"); 
      $result = mysql_query($query, $con) or die(mysql_error($con)); 
      $rows = mysql_fetch_assoc($result); 
      if ($rows) { 
       $arr_key = array_keys($rows); 
       fputcsv($fp, $arr_key); 
       while($rows = mysql_fetch_assoc($result)) 
       { 
        $date_row = checkCsvQuotes($rows['DATE']); 
        $author_row = checkCsvQuotes($rows['AUTHOR']); 
        $message_row = checkCsvQuotes($rows['MESSAGE']); 
        $likes_row = checkCsvQuotes($rows['TOTAL LIKES']); 
        $comments_row = checkCsvQuotes($rows['TOTAL COMMENTS']); 
        $arr_value = array(); 
        $arr_value[] = $date_row; 
        $arr_value[] = $author_row; 
        $arr_value[] = $message_row; 
        $arr_value[] = $likes_row; 
        $arr_value[] = $comments_row; 
        //print_r($arr_value); 
        fputcsv($fp, $arr_value); 
       } 
      } 
      else 
      { 
       $noData = array("DO RECORDS FOUND."); 
       fputcsv($fp, $noData); 
      } 
      fputcsv($fp, $brline); 
     endforeach; 
     fclose($fp); 
     echo "<a href=serp_csvs_page/".$filename_csv." class='btn btn-primary'> View File </a>"; 
     endif; 
     function get_page_name_by_id($id) 
     { 
      $query = mysql_query("SELECT page_id FROM tbl_pages WHERE `id` = '$id'"); 
      $result = mysql_fetch_assoc($query); 
      return $result; 
     } 
    function checkCsvQuotes($string) 
    { 
     if (strpos($string,'"') !== false) { 
      return '"'.str_replace('"','""',$string).'"'; 
     } elseif (strpos($string,',') !== false) { 
      return '"'.$string.'"'; 
     } else { 
      return $string; 
     } 
    } 
     ?> 

林不知道,即使其可能的,因爲即時通訊新的與csv。如果你們有任何想法和我一起出現,我將不勝感激。謝謝。

回答

0

您需要繼續擴展數組,而不是立即吐出元素。我已經改變了你的代碼,但是因爲我無法測試它,所以很可能不起作用。無論哪種方式,比較代碼,以瞭解需要做什麼的想法。

<?php 
if(isset($_POST['submit'])): 
    set_time_limit(99999999); 
    ini_set('memory_limit', "9999M"); 
    $filename_csv = "export_".rand(1000000,9999999)."_".rand(1000000,9999999).".csv"; 
    $path = "serp_csvs_page/".$filename_csv; 
    $fp = fopen($path, 'w'); 
    $page_ids = $_POST['page_id']; 
    $date_from = $_POST['date_from']; 
    $date_to = $_POST['date_to']; 

    $data_ar = array(); //create a "master" array 
    $i = $j = 0; 

    foreach($page_ids as $page_id): 
     $brline = array("\n"); 
     $page_name = get_page_name_by_id($page_id); 
     $dates = checkCsvQuotes("DATE: FROM {$date_from} TO {$date_to}") ; 
     $page_name = checkCsvQuotes("PAGE ID: {$page_name['page_id']}"); 
     $header = array($dates, "\n", $page_name); 
     fputcsv($fp, $header); 
     fputcsv($fp, $brline); 
     $query = sprintf("SELECT post_date AS `DATE`, post_from AS `AUTHOR`, message AS `MESSAGE`, total_likes AS `TOTAL LIKES`, total_comments AS `TOTAL COMMENTS` FROM tbl_contents_pages WHERE `pid_id` = '$page_id' AND `post_date` >= '$date_from' AND `post_date` <= '$date_to'"); 
     $result = mysql_query($query, $con) or die(mysql_error($con)); 
     $rows = mysql_fetch_assoc($result); 
     if ($rows) { 
      $arr_key = array_keys($rows); 
      fputcsv($fp, $arr_key); 
      $i = 0; //set to 0 on each iteration over $page_ids 
      while($rows = mysql_fetch_assoc($result)) 
      { 
       $date_row = checkCsvQuotes($rows['DATE']); 
       $author_row = checkCsvQuotes($rows['AUTHOR']); 
       $message_row = checkCsvQuotes($rows['MESSAGE']); 
       $likes_row = checkCsvQuotes($rows['TOTAL LIKES']); 
       $comments_row = checkCsvQuotes($rows['TOTAL COMMENTS']); 
       if ($j === 0) //note: this assumes all files have the same amount of data 
        $data_ar[$i] = array(); 
       $data_ar[$i][] = $date_row; //these will add to array data instead of creating new array rows 
       $data_ar[$i][] = $author_row; 
       $data_ar[$i][] = $message_row; 
       $data_ar[$i][] = $likes_row; 
       $data_ar[$i][] = $comments_row; 
       //print_r($arr_value); 
       $i++; 
      } 
     } 
     else 
     { 
      $noData = array("DO RECORDS FOUND."); 
      fputcsv($fp, $noData); 
     } 
     $j++; 
    endforeach; 
    for ($q = 0; $q < count($data_ar); $q++) { //insert data to file once the data has been gathered 
     fputcsv($fp, $data_ar[$q]); 
     fputcsv($fp, $brline); 
    } 
    fclose($fp); 
    echo "<a href=serp_csvs_page/".$filename_csv." class='btn btn-primary'> View File </a>"; 
    endif; 
?> 
+0

thnk you mustafa ...會讓你知道它是否可以.. :) –