DLH
DLH

Reputation: 67

PHP radio buttons, multidimensional array

Alright, I don't have any clue what I'm doing. I have set up my multidimensional array correctly I think. The problem I'm having is when you select the radio button, the results need to come out. I'm a complete noob at this PHP stuff....

Here is my radio buttons 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>

And here is my php..... I am a complete noob at this. This is my first "assignment". I'm having too much trouble over this. Some pointers that would steer me the right way would be wonderful.

<?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'];
}


?>

A little bit long, I know... if someone could steer me the right way I would be so grateful!!!!!

Upvotes: 1

Views: 743

Answers (1)

Web Sticklers
Web Sticklers

Reputation: 82

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

Upvotes: 1

Related Questions