下面是我的代码,该代码说明如何从下拉菜单上的阵列中打印或显示一个数据。数据存储在API中。我为代码使用for循环。但是,当我使用下面的代码运行时,它显示多个下拉框。我已经在寻找解决方案,但是仍然没有解决我的问题。预先感谢您的帮助。
<div class="select" id="new-input" onselect="SelectStudents()">
<?php $i = 0; foreach ($arr['data'] as $key => $value) { ?>
<select required="<?php echo ($i == 0) ? 'required':''; ?>">
<option value="<?php echo $value['studentName']; ?>">
<?php echo $value['studentName']; ?>
</option>
</select>
<?php $i++; } ?>
</div>
// Data From API
"data": [
{
"TeacherID": "123",
"TeacherName": "Miss Sara",
"StudName": "Nina Simon",
},
{
"TeacherID": "124",
"TeacherName": "Madam Downy",
"StudName": "Mike Dave",
},
{
"TeacherID": "125",
"TeacherName": "Mr. Adam",
"StudName": "Jenny William",
}
]
答案 0 :(得分:0)
您的<select>
标签位于循环内,因此代码为循环的每次迭代生成一个选择元素。
尝试一下:
<div class="select" id="new-input" onselect="SelectStudents()">
<select required>
<?php foreach ($arr['data'] as $key => $value) {
echo '<option value="'.$value['studentName'].'">';
echo $value['studentName'];
echo '</option>';
} ?>
</select>
</div>
就更改列表上的DOM元素而言,选择:使用JavaScript可能更好。但是,如果必须在服务器端执行此操作,则可以使用以下功能来帮助您:
function getStudents($teacherID, $arr) {
foreach ($arr as $key => $record) {
if ($record['TeacherID'] == $teacherID){
return $record['StudName'];
}
}
}
我希望如果我从菜单中单击Sara小姐,它将在下拉菜单的Sara小姐(即Nina Simon)下加载学生的姓名。
因此,根据我的理解,您想要这样的东西。 -我对发生的事情使用了一些假设。 -我将数据硬编码为php变量。 -最初的问题是关于foreach循环的,所以我经常使用。
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">
a{
display: block;
margin-bottom: 1rem;
}
.card{
padding: 1rem;
background-color: #eee;
width: 300px;
}
select{
margin-top: 1rem;
width: 300px;
}
</style>
</head>
<body>
<?php
class StudentsClass
{
public function listStudents($teacherID, $arr) {
foreach ($arr as $key => $record) {
if ($record['TeacherID'] == $teacherID){
return $record['StudName'];
}
}
}
public function listTeachers($arr) {
$s=[];
foreach ($arr as $key => $record) {
$s[$record['TeacherID']] = $record['TeacherName'];
}
return $s;
}
}
class DisplayHTML
{
public function selectHTML($list,$cssClass='') {
$s='';
if ($cssClass!='') $css = 'class="'.$cssClass.'" ' ; else $css = '';
foreach ($list as $key => $item) {
$s.='<option '.$css.'value="'.$item.'">'.$item.'</option>';
}
$s = '<select>'.$s.'</select>';
return $s;
}
public function anchorHTML($list,$cssClass='') {
$s='';
if ($cssClass!='') $css = 'class="'.$cssClass.'" ' ; else $css = '';
foreach ($list as $key => $item) {
$s.='<a '.$css.'href="?teacherID='.$key.'">'.$item.'</a>';
}
return $s;
}
public function divHTML($id,$data) {
$s='<div class="card"> <h2>My Student list</h2><ul id="studentlist"> </ul>';
foreach ($data as $key => $item) {
if ($item['TeacherID']==$id){
foreach ($item['StudName'] as $k => $value) {
$s.='<li>'.$value.'</li>';
}
$s.='<h3>'.$item['TeacherName'].'</h3></div>';
return $s;
}
}
return '<div class="card">
<h2>My Student list</h2><ul><li>-</li></ul>
<h3>--</h3> </div>';
}
}
$arr = [
[
"TeacherID"=> "123",
"TeacherName"=> "Miss Sara",
"StudName"=> ["Nina","Simon"],
],
[
"TeacherID"=> "124",
"TeacherName"=> "Madam Downy",
"StudName"=> ["Mike","Dave"],
],
[
"TeacherID"=> "125",
"TeacherName"=> "Mr. Adam",
"StudName"=> ["Jenny","William"],
]
];
$stlst = new DisplayHTML;
$stdents = new StudentsClass;
echo $stlst->anchorHTML($stdents->listTeachers($arr));
if (isset($_GET['teacherID'])) {
echo $stlst->divHTML($_GET['teacherID'],$arr);
echo $stlst->selectHTML($stdents->listStudents($_GET['teacherID'],$arr));
}
?>
</body>
</html>