分组和显示调查数据

时间:2015-06-29 11:13:34

标签: php mysql

我是一名PHP新手,我有一个简单的数据库供调查使用。

survey
-------
surveyID    name    email
-----------------------------------
1           Peter   peter@gmail.com
2           Mary    mary@gmail.com
-----------------------------------

两个表存储了一些数据 E.g。

answer
-------
surveyID questionID answerID
-----------------------------------
1        1-1        1
1        1-2        2
1        2          2
1        2          3
1        3          3
2        1-1        5
2        1-2        4
2        2          2
2        3          1
2        3          2
-----------------------------------


------------------------------------------------------------
SurveyID | Name | Email            | Q1-1 | Q1-2 | Q2 | Q3 |
------------------------------------------------------------
1        | Peter | peter@gmail.com | 1    | 2    | 2,3| 3  |
2        | Mary  | Mary@gmail.com  | 5    | 4    | 2  | 1,2|
------------------------------------------------------------

某些问题有多个复选框,因此问题可能有多个答案。

如何使用PHP按表分组和显示结果? 像这样:

{{1}}

2 个答案:

答案 0 :(得分:0)

试试这个:

$sql = "SELECT t1.surveyID as survey, t1.name as name, t1.email as email,
    t2.questionID as question, t2.answerID as answer
    FROM survey AS t1
    INNER JOIN answer AS t2 ON t1.surveyID = t2.surveyID";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "<table><tr><th>Survey</th><th>Name</th><th>Email</th><th>Question</th><th>Answer</th></tr>";
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<tr><td>".$row["survey"]."</td><td>".$row["name"]."</td>
        <td>".$row["email"]."</td><td>".$row["question"]."</td><td>".$row["answer"]."</td></tr>";
    }
    echo "</table>";
}

答案 1 :(得分:0)

    $query="SELECT qutionID FROM answer";       
    $suc = mysql_query($query);
    $cnt = mysql_num_rows($suc);

    $Qhead=array();
    $out="";
    $output="";

    if ($cnt > 0) 
    {           
        while ($row = mysql_fetch_array($suc))
        {               
           array_push($Qhead,'Q'.$row['qutionID']);
        }
    }

  $sql = "SELECT survey.surveyID,survey.name,survey.email FROM survey 
                 INNER JOIN answer ON survey.surveyID = answer.surveyID";
  $result = mysql_query($sql);
  $cnt2 = mysql_num_rows($result);
  $data = "<table><tr><th>SurveyID</th><th>Name</th><th>Email</th>";
                 for($b=0;$b<count($Qhead);$b++)
                 {
                    $out.="<th>".$Qhead[$b]."</th>";
                 }
  $data.=$out;
  $data.="</tr>";
  if ($cnt2 > 0) {  
      while ($row = mysql_fetch_array($result)) 
        {   
           $survey_id= $row['surveyID'];         
           $name= $row['name'];
           $email = $row['email'];
           $data.="<tr><td>" . $row['surveyID'] . "</td>" .              
                    "<td>" . $row['name']  . "</td>".
                    "<td>" . $row['email']  . "</td>";
             for($b1=0;$b1<count($Qhead);$b1++)
                 {
                    $str=$Qhead[$b1];
                    $output.="<td>.$str.</td>";
                  }
    $data.=$output;
    $data.="</tr>";
    }
 $data.="</table>";
 }

试试这个...