gpt4 book ai didi

php - 在 ul 中输出每个数组

转载 作者:行者123 更新时间:2023-11-30 22:01:26 27 4
gpt4 key购买 nike

所以我有一个程序可以获取有关存储在数据库中的所有团队的数据。当我获取数据时,我将信息存储在一个数组中。数据是 leader_idstudent_id 的。所以我要做的是将每个数组输出到一个单独的 ul 中。

团队数据库

id      | leader_id | student_id
1 | 123 | 0987
2 | 123 | 1234345
3 | 97678 | 56766
4 | 97678 | 67558

因此每个 leader_id 可以有 1 个或多个 student_id

此外,如果一个 student_id 具有相同的 leader_id,则它与具有相同的所有其他 student_id 在同一个团队中leader_id

我想做什么

<ul>

<li>123</li>
<li>0987</li>
<li>1234345</li>

</ul>

<ul>

<li>97678</li>
<li>56766</li>
<li>67558</li>

</ul>

我目前得到的

<ul>

<li>123</li>
<li>0987</li>
<li>1234345</li>
<li>97678</li>
<li>56766</li>
<li>67558</li>

</ul>

我尝试过的

我试过这样做(将循环放在 ul 中):

<ul>

<?php

foreach ($students_arr as $student) {
echo "
<li>$student</li>
";
}

?>

</ul>

输出这个(只有一个团队):

<ul>
<li>123</li>
<li>0987</li>
<li>1234345</li>
</ul>

我的 PHP

<!DOCTYPE html>
<html>
<head>
<style type="text/css">

* {
margin: 0;
padding: 0;
}

ul {
padding: 20px;
display: inline-block;
}

li {
list-style: none;
padding: 10px;
}

</style>
</head>
<body>

<ul>

<?php

// GET TEAMS

// turn error reporting off
error_reporting(0);

// get connect.php file
require '../connect.php';

// get all team data
$team_data = mysqli_query($link, "SELECT leader_id, GROUP_CONCAT(student_id SEPARATOR ', ')
AS students_id FROM teams GROUP BY leader_id;");

// check to see if query works
if($team_data) {

while($team = mysqli_fetch_array($team_data)) {

// output the teams data
$leader_id = $team['leader_id'];
$students_id = $team['students_id'];

$students_arr = explode(", ",$students_id);
$leader_first = array_unshift($students_arr, $leader_id);

foreach ($students_arr as $student) {
echo "
<li>$student</li>
";
}
}

} else {

header("Location: ../../admin.php?message=Sorry we could not get the data");

}

?>

</ul>

</body>
</html>

最佳答案

<?php

// GET TEAMS

// turn error reporting off
error_reporting(0);

// get connect.php file
require '../connect.php';

// get all team data
$team_data = mysqli_query($link, "SELECT leader_id, student_id FROM teams order by leader_id, student_id;");

// check to see if query works
if(!$team_data) {
header("Location: ../../admin.php?message=Sorry we could not get the data");
}

// build an associative array of data, keep your data rendering and sql separate
$data = [];

while($team = mysqli_fetch_array($team_data)) {
$leader_id = $team['leader_id'];
$student_id = $team['student_id'];
$data[$leader_id][] = $student_id;
}


// no sql beyond here
?>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">

* {
margin: 0;
padding: 0;
}

ul {
padding: 20px;
display: inline-block;
}

li {
list-style: none;
padding: 10px;
}

</style>
</head>
<body>

<?php foreach ($data as $leader_id => $students): ?>
<ul>
<li><strong><?php echo $leader_id; ?></strong></li>
<?php foreach ($students as $student_id): ?>
<li><?php echo $student_id; ?></li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>

</body>
</html>

关于php - 在 ul 中输出每个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43219493/

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