PHP单选按钮,多维数组

PHP单选按钮,多维数组,php,arrays,multidimensional-array,radio-button,radio,Php,Arrays,Multidimensional Array,Radio Button,Radio,好吧,我不知道我在做什么。我想我已经正确地设置了多维数组。我遇到的问题是,当你选择单选按钮时,结果需要出来。我完全不懂PHP的东西 这是我的单选按钮html: <h2>List animals by </h2> <ul> <li>Habitat: (This selection doesn't work in the demonstration) <form action="" method="post"> <input type

好吧,我不知道我在做什么。我想我已经正确地设置了多维数组。我遇到的问题是,当你选择单选按钮时,结果需要出来。我完全不懂PHP的东西

这是我的单选按钮html:

<h2>List animals by </h2>
<ul>
<li>Habitat: (This selection doesn't work in the demonstration)
<form action="" method="post">
<input type="radio" name="Habitat" value="Forest"> Forest
<input type="radio" name="Habitat" value="Farm"> Farm
<input type="radio" name="Habitat" value="Desert"> Desert
<input type="submit" name="submit" value="List Animals">
</form>
<li>
Food:
<form action="" method="post">
<input type="radio" name="Food" value="Meat" ?prefix=Meat> Meat
<input type="radio" name="Food" value="Grass" ?prefix=Grass> Grass
<input type="radio" name="Food" value="Mixed" ?prefix=Mixed> Mixed
<input type="submit" name="submit" value="List Animals">
</form>
</ul>
<hr>
按名称列出动物
  • 栖息地:(此选项在演示中不起作用) 森林 农场 沙漠
  • 食物: 肉 草 混合的

这是我的php。。。。。我完全不知道这件事。这是我的第一个“任务”。我有太多的麻烦了。一些指引我正确方向的指针会很好

<?php

if (isset($_post['submit'])){
if (isset($_post['radio']))



$animalList = array ();
    $animalList[0] = array ();
        $animalList[0] ['Animal'] = "Bear";
        $animalList[0] ['Habitat'] = "Forest";
        $animalList[0] ['Food'] = "Meat";
    $animalList[1] = array();
        $animalList[1] ['Animal'] = "Deer";
        $animalList[1] ['Habitat'] = "Forest";
        $animalList[1] ['Food'] = "Grass";
    $animalList[2] = array();
        $animalList[2] ['Animal'] = "Pig";
        $animalList[2] ['Habitat'] = "Farm";
        $animalList[2] ['Food'] = "Mixed";
    $animalList[3] = array();
        $animalList[3] ['Animal'] = "Cow";
        $animalList[3] ['Habitat'] = "Farm";
        $animalList[3] ['Food'] = "Grass";
    $animalList[4] = array();
        $animalList[4] ['Animal'] = "Sheep";
        $animalList[4] ['Habitat'] = "Farm";
        $animalList[4] ['Food'] = "Grass";
    $animalList[5] = array();
        $animalList[5] ['Animal'] = "Camal";
        $animalList[5] ['Habitat'] = "Desert";
        $animalList[5] ['Food'] = "Grass";
    $animalList[6] = array();
        $animalList[6] ['Animal'] = "Scorpion";
        $animalList[6] ['Habitat'] = "Desert";
        $animalList[6] ['Food'] = "Meat";



function showAnimals($prefix_requested){

    global $animalList;

    $tbl = "<table border=1>";
    $tbl = $tbl."<tr><th>Animal</th><th>Habitat</th><th>Food</th></tr>";

    foreach ($animalList as $animal){

        if ($animal['Animal'] == $prefix_requested){

    $tbl .= "<tr><td>{$animal['Animal']} 
             {$animal['Habitat']}</td><td>           
             {$animal['Food']}</td></tr>";
        }
    }

    $tbl .="</table>";
    echo $tbl;
    }


    echo "".$_post['radio'];
}


?>

有点长,我知道。。。如果有人能引导我走上正确的道路,我将不胜感激


<?php
function showAnimals($seloption, $prefixsel){
    $animalList = array ();
    $animalList[0] ['Animal'] = "Bear";
    $animalList[0] ['Habitat'] = "Forest";
    $animalList[0] ['Food'] = "Meat";
    $animalList[1] ['Animal'] = "Deer";
    $animalList[1] ['Habitat'] = "Forest";
    $animalList[1] ['Food'] = "Grass";
    $animalList[2] ['Animal'] = "Pig";
    $animalList[2] ['Habitat'] = "Farm";
    $animalList[2] ['Food'] = "Mixed";
    $animalList[3] ['Animal'] = "Cow";
    $animalList[3] ['Habitat'] = "Farm";
    $animalList[3] ['Food'] = "Grass";
    $animalList[4] ['Animal'] = "Sheep";
    $animalList[4] ['Habitat'] = "Farm";
    $animalList[4] ['Food'] = "Grass";
    $animalList[5] ['Animal'] = "Camal";
    $animalList[5] ['Habitat'] = "Desert";
    $animalList[5] ['Food'] = "Grass";
    $animalList[6] ['Animal'] = "Scorpion";
    $animalList[6] ['Habitat'] = "Desert";
    $animalList[6] ['Food'] = "Meat";

    $tbl = "<table border=1>";
    $tbl = $tbl."<tr><th>Animal</th><th>Habitat</th><th>Food</th></tr>";
    foreach ($animalList as $animal){
        if ($animal[$prefixsel] == $seloption){
            $tbl .= "<tr><td>".$animal['Animal']."</td><td>".$animal['Habitat']."</td><td>".$animal['Food']."</td></tr>";
        }
    }
    $tbl .="</table>";
    echo $tbl;
}
if (isset($_POST['submit1'])){
    showAnimals($_POST['Habitat'], 'Habitat');
}
if (isset($_POST['submit2'])){
    showAnimals($_POST['Food'], 'Food');
}
?>
<h2>List animals by </h2>
<ul>
<li>Habitat: (This selection doesn't work in the demonstration)
<form action="" method="post">
<input type="radio" name="Habitat" value="Forest" <?php echo ($_POST['Habitat']=='Forest'?'checked="checked"':'');?>> Forest
<input type="radio" name="Habitat" value="Farm" <?php echo ($_POST['Habitat']=='Farm'?'checked="checked"':'');?>> Farm
<input type="radio" name="Habitat" value="Desert" <?php echo ($_POST['Habitat']=='Desert'?'checked="checked"':'');?>> Desert
<input type="submit" name="submit1" value="List Animals">
</form>
<li>
Food:
<form action="" method="post">
<input type="radio" name="Food" value="Meat"  <?php echo ($_POST['Food']=='Meat'?'checked="checked"':'');?>> Meat
<input type="radio" name="Food" value="Grass" <?php echo ($_POST['Food']=='Grass'?'checked="checked"':'');?>> Grass
<input type="radio" name="Food" value="Mixed" <?php echo ($_POST['Food']=='Mixed'?'checked="checked"':'');?>> Mixed
<input type="submit" name="submit2" value="List Animals">
</form>
</ul>
<hr>
动物名录
  • 栖息地:(此选项在演示中不起作用)
    我建议用db来做这个谢谢你我下班后会检查一下,看看事情是怎样的谢谢你,它工作得很好。我会投票,但我没有足够的声誉!再次感谢你。