繁体   English   中英

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

[英]PHP - I cannot correctly display data in xls file (exported from mysql))

我正在使用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;

该代码段与您的示例数据不相似。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM