The data in the table seems to be in JSON format. You can convert it to a PHP object using json_decode()
then print that object using print_r()
.
When put all together, you get
echo print_r(json_decode($data), true);
Note, I have to edit this response back to what it is now because @fenil-shah had changed it some something else that was not suitable. Please send your own response instead of making others say what they didn't intend to.
@fenil-shah, my answer was written the way it was on purpose. Here are the explanations:
json_decode($data,true)
. You need to know that the space was there for two reasons: Readability and Compliance with the PSR-2 standard (read more about that PHP coding standard at https://www.php-fig.org/psr/psr-2/#46-method-and-function-calls ). php中有echo ""
, print_r()
和print()
函数可用于打印数据。如果查询返回数组,则使用print_r()
函数,但是如果查询未返回任何内容或php中的无效提取方法,则数据无法获取通过任何这些功能显示。
I think this is what you want as a output. You can decode the JSON received from database using json_decode()
function and get your result transformed in associative array. Later on passing the array in foreach()
loop will do your work accordingly
<?php
$result = '{
"30": {
"subject_id":343,
"correct_answers":34,
"wrong _answers":61,
"not_answered":0,
"time_spent":3801,
"time_to_spend":3680,
"time_spent_correct_ answers":3286,
"time_spent_wrong_answers":515
},
"52": {
"subject_id":52,
"correct_answers":7,
"wrong_answers":3,
"not_answered":0,
"time_spent":883 ,
"time_to_spend":94343,
"time_spent_correct_ans wers":352,
"time_spent wrong_answers":441
},
"53": {
"subject_id":53,
"correct_answers":3,
"wrong_answers":7,
"not_answered":43,
"time_spent":584 ,
"time_to_spend":900,
"time_spent_correct_ans wers":154,
"time_spent wrong_answers":430
}
}';
$json_decoded_data = json_decode($result,true);
?>
<table>
<tbody>
<tr>
<th>subject_id</th>
<th>correct_answers</th>
<th>wrong_answers</th>
</tr>
<?php
foreach($json_decoded_data as $row){
echo "<tr>";
echo "<td>".$row['subject_id']."</td>";
echo "<td>".$row['correct_answers']."</td>";
echo "<td>".$row['correct_answers']."</td>";
echo "</tr>";
}
?>
</tbody>
</table>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.