Simple Form + PHP Array
Posted: Mon Sep 21, 2009 7:40 pm
Hello all, another nooby assignment for noobie me.
I have to get this form to work using a Function. Here is what the professor is asking:
* Build a multidimensional array storing animal information as below. The array should be placed outside of the function.
* Produce a function, showAnimal, which will take an argument for the type of food an animal consumes. This function should produce a animal list based on the food type specified by the function call. Use a form to take in user input on the food types and respond the user with the correponding animal list. Note that the function needs to be scalable solution. That is to say when additonal animals are added, the only change you need to make to the script is to add them to the array and nothing should be modified inside the function.
* Extra points
Up to 5 extra points will be given if you can make the habitat selection works, too.
Here is a working example of how mine should look: http://omega.uta.edu/~cyjang/ctec4321/l ... answer.php
Here is what I have so far, I have been told that I must use this multidimensional array format for now. So please help me stick to this way, I am aware of using associated arrays instead.
I have to get this form to work using a Function. Here is what the professor is asking:
* Build a multidimensional array storing animal information as below. The array should be placed outside of the function.
* Produce a function, showAnimal, which will take an argument for the type of food an animal consumes. This function should produce a animal list based on the food type specified by the function call. Use a form to take in user input on the food types and respond the user with the correponding animal list. Note that the function needs to be scalable solution. That is to say when additonal animals are added, the only change you need to make to the script is to add them to the array and nothing should be modified inside the function.
* Extra points
Up to 5 extra points will be given if you can make the habitat selection works, too.
Here is a working example of how mine should look: http://omega.uta.edu/~cyjang/ctec4321/l ... answer.php
Here is what I have so far, I have been told that I must use this multidimensional array format for now. So please help me stick to this way, I am aware of using associated arrays instead.
Code: Select all
<BODY>
<h2>List animals by </h2>
<ul>
<li>Habitat: (Doesn't work)
<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="Diet" value="Meat"> Meat
<input type="radio" name="Diet" value="Grass"> Grass
<input type="radio" name="Diet" value="Mixed"> Mixed
<input type="submit" name="submit" value="List Animals">
</form>
</ul>
<hr>
<?php
$animals=array();
$animals[0]=array();
$animals[0]['Type']='Bear';
$animals[0]['Where']='Forest';
$animals[0]['Diet']='Meat';
$animals[1]=array();
$animals[1]['Type']='Deer';
$animals[1]['Where']='Forest';
$animals[1]['Diet']='Grass';
$animals[2]=array();
$animals[2]['Type']='Pig';
$animals[2]['Where']='Farm';
$animals[2]['Diet']='Mixed';
$animals[3]=array();
$animals[3]['Type']='Cow';
$animals[3]['Where']='Farm';
$animals[3]['Diet']='Grass';
$animals[4]=array();
$animals[4]['Type']='Sheep';
$animals[4]['Where']='Farm';
$animals[4]['Diet']='Grass';
$animals[5]=array();
$animals[5]['Type']='Camal';
$animals[5]['Where']='Desert';
$animals[5]['Diet']='Grass';
$animals[6]=array();
$animals[6]['Type']='Scorpion';
$animals[6]['Where']='Desert';
$animals[6]['Diet']='Meat';
//
function showAnimal($thisPrefix)
{
//
global $animals;
foreach ($animals as $animal)
{
//
if ($animal['Diet'] == $thisDiet){
//
$Type = $animal['Type'];
$Where = $animal['Where'];
$Diet = $animal['Diet'];
//
$list = $list."<tr><td>$Type</td><td>$Where</td><td>$Diet</td></tr>";
}
}
//
$list = $list.'</table>';
//
echo $list;
?>