PHP每个选择框回显一个特定的数组

时间:2016-11-09 18:54:05

标签: php html html5

我是PHP的新手并试图完成一个实践。 但我不能让我工作,它将回应所选复选框的数组 假设我选择了Schaatsen,我希望他们在屏幕上看到阵列schaatsen。 已经感谢您的帮助。

<!DOCTYPE html>
<html>
   <head>
      <meta charset="UTF-8">
      <title>Merijn</title>
   </head>
   <body>
        <form action="" method="GET">
            <select name="option">
                <option value="schaatsen">Schaatsen</option>
                <option value="alpineskieen">Alpineskieen</option>
                <option value="snowboarden">Snowboarden</option>    
                <option value="ijshockey">IJshockey</option>
                <option value="rodelen">Rodelen</option>        
            </select>
            <input type="submit" name="submit">
        </form>
      <?php
         $evenementen = array
         (
        "schaatsen"=>array
               (
               "Heren 500 meter",
               "Heren 1000 meter",
               "Heren 1500 meter",
               "Heren 5 Kilometer",
               "Vrouwen 500 meter",
               "Vrouwen 1000 meter",
               "Vrouwen 1500 meter",
               "Vrouwen 3000 meter"
               ),
        "alpineskieen"=>array
               (
               "Slalom Mannen",
               "Slalom Vrouwen",
               "Reuzenslalom Mannen",
               "Reuzenslalom Vrouwen",
               "Super-G Mannen",
               "Super-G Vrouwen"
               ),
        "snowboarden"=>array
               (
               "Slopestyle Mannen",
                       "Slopestyle Vrouwen",
               "Halfpipe Mannen",
               "Halfpipe Vrouwen",
               "Boardcross Mannen",
               "Boardcross Vrouwen",
               "Freestyle Mannen",
               "Freestyle Vrouwen"
               ),
        "ijshockey"=>array
               (
               "Kwalificatie Rondes",
                       "1/8 Finale",
               "1/4 Finale",
               "1/2 Finale",
               "Finale"
               ),
        "rodelen"=>array
               (
               "Individueel Mannen",
                       "Individueel Vrouwen",
               "Dubbel Mannen",
               "Dubbel Vrouwen",
               "Teamestafette"
               )
            );

            if(isset($_GET["option"])){
                $option = $_GET["option"];
                switch ($option){
                    case "schaatsen":
                        echo $evenementen["schaatsen"];
                    break;
                    case "alpineskieen":
                        echo $evenementen["alpineskieen"];
                    break;
                    case "snowboarden":
                        echo $evenementen["snowboarden"];
                    break;
                    case "ijshockey":
                        echo $evenementen["ijshockey"];
                    break;
                    case "rodelen":
                        echo $evenementen["rodelen"];
                    break;
                }
            }
         ?>
   </body>
</html>

1 个答案:

答案 0 :(得分:0)

从URL参数中读取$_GET['option'],告诉PHP数组将使用的内容。

<?php
if( isset($_GET["option"]) ){
    $option = $_GET["option"];
    switch ($option){
        case "schaatsen":
            $child = $evenementen["schaatsen"];
        break;
        case "alpineskieen":
            $child = $evenementen["alpineskieen"];
        break;
        case "snowboarden":
            $child = $evenementen["snowboarden"];
        break;
        case "ijshockey":
            $child = $evenementen["ijshockey"];
        break;
        case "rodelen":
            $child = $evenementen["rodelen"];
        break;
    }
} ?>

<select name="child-option">
    <?php foreach ($child as $item) { ?>
        <option><?php $item ?></option>
    <?php } ?>
</select>

对每个选项使用foreach( $child as $item )

参见参考:PHP foreach