首页 > 解决方案 > 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:

MYSQL Tables

What I want to do with this mysql-table is, post it as a html-table which should look something like this:

output table

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 html output so far

But I want it to look like this desired html output

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.

标签: phpmysqlhtml-tablevoting

解决方案


推荐阅读