PHP单选按钮,多维数组

时间:2014-09-17 04:38:54

标签: 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="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'];
}


?>

有点长,我知道......如果有人能以正确的方式引导我,我会非常感激!!!!!

1 个答案:

答案 0 :(得分:1)

<?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>