在html表中标记某个单元格-(投票表)

4ktjp1zp  于 2021-06-24  发布在  Mysql
关注(0)|答案(0)|浏览(169)

我找了好几天来解决我的问题我有不同的想法,但似乎没有一个像预期的工作。
我的问题是:我有一些mysql表如下所示:

我想对这个mysql表做的是,将其作为一个html表发布,该表应该如下所示:

我的问题是,我不知道用户的数量,他们投票给一个答案,我甚至不知道答案的数量。
我只想用x来标记表格单元格,用户在其中选择了答案。
这是最接近我预期目标的代码。

$sql = "SELECT voting_user.*, voting_user_answer.*, voting_answeren.* FROM
voting_user, voting_user_answer, voting_answeren WHERE  
voting_user_answer.thema_id = $thema_id AND voting_user.user_id = 
voting_user_answer.user_id AND voting_answeren.answer_id = 
voting_user_answer.answer_id";

$i=0;   
echo "<table border=1>";
echo "<tr><th>answer-ID</th><th>answer</th><th>user</th>";
foreach ($pdo->query($sql) as $row) {

echo "<tr><td>".$row["answer_id"]."</td>
<td>".$row["answer_option"]."</td><td>".$row["user_name"]."   
</td></tr>";
$tableRows[$i]=array("answer_option"=>$row["answer_option"],
"user_name"=>$row["user_name"], "cross"=>"x");
$namen[$i]=$row["user_name"];
$answer = Array
(
"answer_option" => $row["answer_option"]
, "test2" => "x"
);

$i++;
$counter=$i-1;
}
echo "</table>";

$resultMatrix = array();

foreach($tableRows as $resulting) {
$from = $resulting['answer_option'];
$to = $resulting['user_name'];
$cross = $resulting['cross'];

$resultMatrix[$from][$to] = $cross;
}

echo '<table border="1">';
echo '<tr>';
echo '<th>', '#', '</th>';

foreach($namen as $user_name) { 
echo '<th>', $namen[$i] ,'</th>';
}
echo '</tr>';

foreach(array_keys($resultMatrix) as $answer_option) { 
echo '<tr>';
echo '<td>', $answer_option, '</td>'; 
foreach(array_keys($resultMatrix[$answer_option]) as $user_name) { 
echo '<td>', $resultMatrix[$answer_option][$user_name], '</td>';    
}

echo '</tr>';
}
echo '</table>';

哪怕是最细微的暗示,我都会感激的。

编辑

到目前为止,我的html输出是这样的

但我希望它看起来像这样

上表只是为了澄清,这样你就可以看到投票结果的样子。
我的问题是,我不知道有多少用户在投票,也不知道用户可以从中选择多少答案。这将因民意测验而异。所以我不能用静态表来标记单元格。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题