PHP - 我无法在xls文件中正确显示数据(从mysql导出))

时间:2012-01-11 20:47:18

标签: php mysql sql

我正在使用php。我想将一些数据从mysql数据库导出到xls文件。

我在mysql数据库中的表答案:

表答案:

Id    Registration_id    Question_id    Answer
1     Reg01                 1            John
2     Reg01                 2            Smith
3     Reg01                 3            name@domain.com
4     Reg02                 1            Rachel
5     Reg02                 2            Smith
6     Reg02                 3            name2@domain.com 

我从下面的代码中获得的结果不是我预期的结果。标题显示正确,但答案显示在同一行...

名称SecondName电子邮件    John Smith name@domain.com Rachel Smith name2@domain.com

我想要这个结果:

Name   SecondName  Email
John    Smith      name@domain.com  
Rachel  Smith      name2@domain.com

你能告诉我怎么办?

感谢。

以下是代码:

$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;

1 个答案:

答案 0 :(得分:1)

代码段与您的示例数据没有任何相似之处。

您正在将每个结果写入一个新的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; }作为循环中的第一个动作