php - Mark a certain cell in a html table - (voting table)
问题描述
I am searching for days to get my problem solved I got different ideas but none seems to work as expected.
My problem: I have some mysql tables which look like this:
What I want to do with this mysql-table is, post it as a html-table which should look something like this:
My problem is that I don't know the number of users, who are voting for an answer and I even don't know the number of answers.
I just want to mark the table cell with an x, in which the user had chosen the answer.
This is the code that comes the nearest to my intended goal.
$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>';
I would be grateful for even the slightest hints.
edit
My html output so far, looks like that
But I want it to look like this
The upper table is just for clarification, so that you can see how the voting results are looking.
My problem is that I don't know how many users are voting and how many answers the user can chose from. That will differ from poll to poll. So I can't use a static table to mark the cell.
解决方案
推荐阅读
- android - 广播接收器中的权限拒绝
- java - 没有这样的表:QuestionDB.db
- c# - 尝试激活 'xxx.Infrastructure.TagRepository' 时无法解析类型 xxx.'Infrastructure' 的服务
- sql - 为什么我们使用连接从多个表中获取数据?
- ios - 当应用程序处于后台或运行状态而不是应用程序终止时处理推送通知
- python-3.x - cairocffi 在 Ubuntu 18.04 上安装失败
- java - 防止反编译/逆向工程 Android APKs
- android - 如何在不与 MainActivity 交互的情况下从通知中打开片段页面?
- git - 将 gitlab 存储库复制到 Bitbucket,包括子模块
- android - 在android画布中使用cubicTo绘制云形状