2012-01-11 39 views
0

我正在使用php。我想從mysql數據庫導出一些數據到xls文件。PHP - 我無法正確顯示xls文件中的數據(從mysql導出))

表答案我有在MySQL數據庫:

表答案:

Id Registration_id Question_id Answer 
1  Reg01     1   John 
2  Reg01     2   Smith 
3  Reg01     3   [email protected] 
4  Reg02     1   Rachel 
5  Reg02     2   Smith 
6  Reg02     3   [email protected] 

i。從下面的代碼獲得的結果爲沒有結果的我的預期。標題是顯示正常,但答案都顯示在同一行...

名稱SecondName電子郵件 約翰·史密斯[email protected]蔡玉嫺[email protected]

我想這樣的結果:

Name SecondName Email 
John Smith  [email protected] 
Rachel Smith  [email protected] 

你能告訴我該怎麼做?

謝謝。

下面是代碼:

$conn = mysql_connect("localhost","form","pass"); 
$db = mysql_select_db("form",$conn); 


$query = "SELECT DISTINCT wp_events_question.question as qst, wp_events_answer.answer as ans 
FROM wp_events_question, wp_events_answer 
WHERE wp_events_question.id = wp_events_answer.question_id"; 

$query2 = "SELECT wp_events_question.question as qst, wp_events_answer.answer as ans 
FROM wp_events_question, wp_events_answer 
WHERE wp_events_question.id = wp_events_answer.question_id and wp_events_detail.id = wp_events_attendee.event_id AND wp_events_attendee.id = wp_events_answer.attendee_id"; 

$result = mysql_query($query) or die(mysql_error()); //Headers 

$result2 = mysql_query($query2) or die(mysql_error()); //Answers 

    //Headers 
      $tbl= " <table border='1'>"; 

      $tbl= $tbl . "<tr height='50px'>"; 

      while($row = mysql_fetch_array($result)) 
      {  
       $tbl= $tbl . "<td WIDTH='50px' align='center'>".$row['qst']."</td>";     
      } 



    //Answers  
       $tbl = $tbl . "</tr>"; 
       $tbl = $tbl . "<tr>"; 
      while($row2 = mysql_fetch_array($result2)) 
      { 

       $tbl= $tbl . "<td WIDTH='50px' align='center'>".$row2['ans']."</td>"; 

      } 
      $tbl = $tbl . "</tr>"; 
      $tbl = $tbl . "</table>"; 


      header("Cache-Control: no-stor,no-cache,must-revalidate"); 
      header("Cache-Control: post-check=0,pre-check=0", false); 
      header("Cache-control: private"); 
      header("Content-Type: application/force-download"); 
      header("Content-Disposition: inline; attachment;filename=Reservations.xls"); 
      header("Content-Transfer-Encoding: binary"); 
      header("Pragma: no-cache"); 
      header("Expires: 0"); 

      print $tbl; 
+0

你知道你可以使用'$ tbl。='而不是'$ tbl = $ tbl。 '?剩下的,我沒有Excel,所以我不能測試這個,但我認爲用許多庫中的一個生成一個合適的xls(x)文件,或者失敗了,編寫一個.csv文件更容易被Excel理解,然後HTML。 – Wrikken 2012-01-11 21:03:32

+0

像經常這樣,HTML僞裝成XLS,爲什麼不把它命名爲Reservations.html? Excel仍然可以導入HTML文件:它不會被con。 – 2012-01-11 21:42:48

回答

1

的代碼片段不承擔任何resemblence您的示例數據。

您正在將每個結果寫入一個新的TD,但都在同一個TR中,這就是爲什麼它們都在同一行。

變化:

 $tbl = $tbl . "<tr>"; 
     while($row2 = mysql_fetch_array($result2)) 
     { 

      $tbl= $tbl . "<td WIDTH='50px' align='center'>".$row2['ans']."</td>"; 

     } 
     $tbl = $tbl . "</tr>"; 

 while($row2 = mysql_fetch_array($result2)) 
     { 
      $tbl = $tbl . "<tr>"; 
      $tbl = $tbl . "<td WIDTH='50px' align='center'>".$row2['ans']."</td>"; 
      $tbl = $tbl . "</tr>"; 
     } 

但我不知道爲什麼你兩次執行相同的SQL查詢......只要運行一個循環對以$一個查詢firstRow = true;在循環之前,還有一個if($ firstRow){//顯示你的頭文件; $ firstRow = false; }作爲循環中的第一個動作