Переключатели PHP, многомерный массив

Хорошо, я понятия не имею, что я делаю. Мне кажется, я правильно настроил многомерный массив. У меня проблема, когда вы выбираете переключатель, результаты должны появиться. Я полный нуб в этом 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

Решение

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

Другие решения

Других решений пока нет …