Simple Form + PHP Array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aalbrand
Forum Newbie
Posts: 11
Joined: Tue Sep 15, 2009 12:06 am

Simple Form + PHP Array

Post by aalbrand »

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.

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;
?>
 
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Simple Form + PHP Array

Post by Mirge »

aalbrand wrote: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.

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;
?>
 
So.... what exactly are you having trouble with? Explaining your problem would be helpful. :banghead:
aalbrand
Forum Newbie
Posts: 11
Joined: Tue Sep 15, 2009 12:06 am

Re: Simple Form + PHP Array

Post by aalbrand »

Yes there is problem with my code. It is suppose to work like this one: http://omega.uta.edu/~cyjang/ctec4321/l ... answer.php

Here is mine currently and the code to go with it: http://omega.uta.edu/~aea8532/4321/animalList_aa.php

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($thisAnimal)
{
    //
    global $animals; 
 
    $list = '<table border=1>';
    $list = $list.'<tr> <td><b>Animal</b></td> <td><b>Habitat</b></td> <td><b>Food</b></td> </tr>';
 
 
    foreach ($animals as $animal)
    {
        //
        if ($animal['Diet'] == $thisAnimal){
            //
            $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;
}
 
//
showAnimal($_POST['Diet']);
 
 
 
 
?>
 
</BODY>
Thanks!
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Simple Form + PHP Array

Post by Mirge »

So which part are you wanting help with? Nobody is going to do your assignment for you..
aalbrand
Forum Newbie
Posts: 11
Joined: Tue Sep 15, 2009 12:06 am

Re: Simple Form + PHP Array

Post by aalbrand »

Obviously I am not asking you to do my assignment, if you don't want to help, don't. I am unsure of where the problem lies, and I am close to completion (again not asking anyone to do my assignment for me). I just have to figure out how to make things show up exactly the same as my professors'.

If you click on both you will see that:

My table titles are showing up before the Submit button is hit.
I have put that asside for a moment to work on the bonus, but the problem still lies with the table output. Although i did get the catagories to show up when you click submit.

Thanks
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Simple Form + PHP Array

Post by Mirge »

Ok, well break it into 2 pieces.

1.) Display the form. This should be shown whether or not the form was already submitted.
2.) The results from the form. This should only be shown when the form is submitted.

#1 is the easy part. You've already done that.
#2 can be done by checking to see that a value is set that's only set when the form is submitted (IE: submit button).

You are spitting out your table in the showAnimal() function which is gets called whether or not the form is submitted. That is a quick fix, so go ahead and get that squared away.

If I had a submit button like: <input type="submit" name="submitButton" value="1"/>, then in PHP I could do something along the lines of:

Code: Select all

 
$submitted = isset($_POST['submitButton']) ? true : false;
 
..
..
rest of code
..
..
 
if($submitted) { showAnimal(); } /* Only call showAnimal() if the form was submitted. */
 
aalbrand
Forum Newbie
Posts: 11
Joined: Tue Sep 15, 2009 12:06 am

Re: Simple Form + PHP Array

Post by aalbrand »

Ok, so I almost got it!

How do i get the Table headers to show up with the actual submit query. So before you submit there is nothing under neath the Form. Also, how would I only do one table instead of 2? So say, how would I Get 1 table for both forms. In short, only the item you submit will show up below.

Code: Select all

<BODY>
 
<h2>List animals by </h2>
<ul>
 
<li>Habitat:
    <form action="" method="post">
        <input type="radio" name="Where" value="Forest"> Forest
        <input type="radio" name="Where" value="Farm"> Farm
        <input type="radio" name="Where" 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($thisAnimal)
{
    //
    global $animals; 
 
 
    $list = '<table border=1>';
    $list = $list.'<tr> <td><b>Animal</b></td> <td><b>Habitat</b></td> <td><b>Food</b></td> </tr>';
 
    foreach ($animals as $animal){
 
        //
        if ($animal['Diet'] == $thisAnimal){
            //
            $Type = $animal['Type'];
            $Where = $animal['Where'];
            $Diet = $animal['Diet'];
            //
        
 
            $list = $list."<tr><td>$Type</td><td>$Where</td><td>$Diet</td></tr>";
        }
 
        if ($animal['Where'] == $thisAnimal){
            //
            $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;
}
 
//
 
showAnimal($_POST['Where']);
showAnimal($_POST['Diet']);
 
 
?>
 
</BODY>
Thanks.
aalbrand
Forum Newbie
Posts: 11
Joined: Tue Sep 15, 2009 12:06 am

Re: Simple Form + PHP Array

Post by aalbrand »

sry we must have submitted at the same time, I will work on it some more and post again.

Thanks
aalbrand
Forum Newbie
Posts: 11
Joined: Tue Sep 15, 2009 12:06 am

Re: Simple Form + PHP Array

Post by aalbrand »

Utterly dont understand, trying things:

--tried putting table outside like you said and either "A", nothing shows up or "B", still dont get right output.

Thanks for trying, I am noobie so I don't understand things without explanation of why things are being added.

Thanks
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Simple Form + PHP Array

Post by Mirge »

Ok, in both forms you have the same name for the submit button:

<input type="submit" name="submit" value="List Animals">

So, in the processing script (which is the same script in this case)... you could check to see if that name is set before CALLING the showAnimals() function which displays the table (unless you've changed this behavior).

if(isset($_POST['submit'])) { showAnimals(); }
Post Reply