gpt4 book ai didi

php - JSON 格式到 HTML 表格

转载 作者:行者123 更新时间:2023-11-30 21:57:03 25 4
gpt4 key购买 nike

我有一段代码读取SQL数据并将其转换成JSON格式

$sql = "SELECT students.studentnumber, students.firstname, students.lastname, badge_id FROM students
INNER JOIN student_has_badge
WHERE student_has_badge.badge_id = 10
AND student.studentnumber = student_has_badge.studentnumber
ORDER BY lastname ASC
LIMIT 50;";

$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));

//creating an array
$emparray = array();
while ($row = mysqli_fetch_assoc($result)) {
$emparray[] = $row;
}

echo json_encode($emparray);
mysqli_close($connection);

我从中得到的输出如下所示:

[{"studentnumber":"11111","firstname":"John","lastname":"Smith","badge_id":"10"},
{"studentnumber":"1","firstname":"Nick","lastname":"Smith","badge_id":"10"},
{"studentnumber":"500740442","firstname":"Jason","lastname":"Baker","badge_id":"10"},
{"studentnumber":"00000","firstname":"Tim","lastname":"Smith","badge_id":"10"}]

现在我想把这些数据放在一个 HTML 表格中,我尝试了各种方法,但没有一个有效,有人能给我指出正确的方向吗?

最佳答案

<?php
$emparray ='[{"studentnumber":"11111","firstname":"John","lastname":"Smith","badge_id":"10"},{"studentnumber":"1","firstname":"Nick","lastname":"Smith","badge_id":"10"},{"studentnumber":"500740442","firstname":"Jason","lastname":"Baker","badge_id":"10"},"studentnumber":"00000","firstname":"Tim","lastname":"Smith","badge_id":"10"}]';

$emparray = json_decode($emparray,1);
echo '<table border="1"><tr><th>Student#<th>First name</th><th>lastname</th><th>badge id</th></tr>';
foreach($emparray as $key => $value){
echo '<tr><td>'.$value['studentnumber'] . '</td><td>'.$value['firstname'] . '</td><td>'.$value['lastname'] . '</td><td>'.$value['badge_id'] .'</td></tr>';
}
echo '</table>';
?>

关于php - JSON 格式到 HTML 表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44806941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com