用php数组替换html列表

时间:2018-10-25 10:04:16

标签: php html

所以我的任务是我需要从一个数组创建一个html列表,并从一个数组中创建一个下拉菜单,并且如果我选择一个特定的学科,它应该只显示该学科及其老师。

我做了这个,但是如果我在数组中添加一个新的主题,它就会中断,所以我需要找到一个解决方案,如果我在数组中添加另一个项目,它应该工作而无需更改代码(我需要使用foreach和if语句)PS:我被困了几个小时,感谢您的帮助。

我的代码:

df2

2 个答案:

答案 0 :(得分:0)

<?php
$School = array(
    'science' => array(
        'teachersmith',
        'anotherteachersmith'
    ),
    'english' => array(
        'mr.vincent',
        'teachermichael',
        'coolestteacherever'
    ),
    'math' => array(
        'mathteacher',
        'mr.jason'
    ),
); 

$final_array = $School; // To not lose the $School array we store a copy as $final_array

    if (isset($_POST['submit'])){
        // We create a new array with $_POST["subjectoptions"] (e.g. science) and subarrays with the 2nd level of $School[$_POST["subjectoptions"]] (e.g. $School['science'] which gives us teachersmith and anotherteachersmith)
        $new_array[$_POST["subjectoptions"]] = $School[$_POST["subjectoptions"]]; 

        // Since we now want to work with the new array we set final_array to the new_array
        $final_array = $new_array;
    } 

    if (isset($_POST['reset'])){
        // If the reset button is clicked we restore the original array
        $final_array = $School;
    } 

echo "<ul class=listclass>";
foreach ($final_array as $subject => $teachername){
    echo "<li class=listclass>"."$subject". "</li>";

    echo "<ul class=listclass>";
    foreach ($teachername as $cNamen){
        echo "<li class=listclass>"."$cNamen". "</li>";
    }
    echo "</ul>";
}
echo "</ul>";
?>

<form method='POST'>
    <select name="subjectoptions">
        <option>Choose a subject</option>
            <?php 
            foreach ($School as $subject2 => $aDocentnames){ 
                echo "<option value=$subject2>$subject2</option>";
            } ?>
    </select>
    <input type="submit" name="submit" value="show selected subject"/>
</form>
<form method='POST'>
    <input type="submit" name="reset" value="show all subjects"/>
</form>

答案 1 :(得分:0)

好吧,实际上,这不是您寻求某些作业帮助的地方。但是您有问题,需要一些帮助来解决。

这是解决方案-

这是您的数组:

<?php
$School = array(
    'science' => array(
        'teachersmith',
        'anotherteachersmith'
    ),
    'english' => array(
        'mr.vincent',
        'teachermichael',
        'coolestteacherever'
    ),
    'math' => array(
        'mathteacher',
        'mr.jason'
    )
); 

在这里,我检查是否提交了表单,还检查了是否选择了主题。如果选择一个主题,那么我将创建一个循环,选择该主题作为该数组的索引。它显示了科目名称和教师列表。

if(isset($_POST['submit']) && !empty($_POST['subjectoptions'])){ $subjectoptions = $_POST['subjectoptions'];?>
    <ul class='listclass'>
        <ol class='listclass'><?php echo $_POST['subjectoptions'];?></ol>
        <?php foreach($School[$subjectoptions] as $teachername){?>
        <li class='listclass'><?php echo $teachername.".";?></li>
        <?php }?>
    </ul>
<?php }?>

这是表格,我使用属性来保持选中状态。

<form method='POST'>
    <select name="subjectoptions">
        <option>Choose a subject</option>
        <?php 
        $subjectName = array_keys($School);
        foreach ($subjectName as $subject){ 
            echo "<option ".(($subject == @$_POST['subjectoptions']) ? 'selected' : '')." value='".$subject."'>".$subject."</option>";
        }?>
    </select>
    <input type="submit" name="submit" value="show selected subject"/>
    <input type="submit" name="reset" value="show all subjects"/>
</form>