基于特定表单选择的PHP显示表数组

时间:2015-09-19 06:15:23

标签: php arrays forms

正如标题所述,我正在尝试显示一个数组表,但只显示基于表单选择的数组表。我将它们过滤掉但是仍然有空行没有显示结果。知道如何不显示没有结果的行吗?

表格

<!DOCTYPE HTML>
<HTML>
<HEAD>
    <TITLE> CTEC 4321: Assignment: Animal List </TITLE>
</HEAD>

<BODY>

<h2>List animals by </h2>
<ul>
    <li>
        Food:
        <form action="multiarray.php" method="post">
            <input type="radio" name="Food" value="Meat"> Meat
            <input type="radio" name="Food" value="Grass"> Grass
            <input type="radio" name="Food" value="Mixed"> Mixed
            <input type="submit" name="submit" value="List Animals">
        </form>
</ul>
<hr>



</BODY>
</HTML>

PHP

<?php
// set up a multi-dimensional array to store information for all classes.
$aList = array ();
$aList[0] = array();
$aList[0]['meatAnimal'] = "Bear";
$aList[0]['meatHabitat'] = "Forest";
$aList[0]['meatFood'] = "Meat";


$aList[1] = array();
$aList[1]['grassAnimal'] = "Deer";
$aList[1]['grassHabitat'] = "Forest";
$aList[1]['grassFood'] = "Grass";

$aList[2] = array();
$aList[2]['mixedAnimal'] = "Pig";
$aList[2]['mixedHabitat'] = "Farm";
$aList[2]['mixedFood'] = "Mixed";

$aList[3] = array();
$aList[3]['grassAnimal'] = "Cow";
$aList[3]['grassHabitat'] = "Farm";
$aList[3]['grassFood'] = "Grass";

$aList[4] = array();
$aList[4]['grassAnimal'] = "Sheep";
$aList[4]['grassHabitat'] = "Farm";
$aList[4]['grassFood'] = "Grass";

$aList[5] = array();
$aList[5]['grassAnimal'] = "Camel";
$aList[5]['grassHabitat'] = "Desert";
$aList[5]['grassFood'] = "Grass";

$aList[6]['meatAnimal'] = "Scorpion";
$aList[6]['meatHabitat'] = "Desert";
$aList[6]['meatFood'] = "Meat";


function showClass($prefix_requested){
    // Use the global keyword to tell the PHP engine to find the variable $classList outside of the function.  Without this, the PHP engine will only look for $classList inside a function.

    global $aList;

    // Set up a variable $tbl to store the HTML output (class list table)
    // Note that the <table></table> tags and the table header row are outside of the foreach loop so that they are not repeated in each iteration of the loop.

    $tbl = "<table border=1>";
    $tbl = $tbl."<tr><th>Animal</th><th>Habitat</th><th>Food</th></tr>";

    // Use a foreach statement to loop through each class in the $classList array
    foreach ($aList as $Animal){
        // for each class, we would like to see if its prefix matches the parameter ($prefix_requested) provided in the function call.

        if (isset($_POST['submit'])) {

            $food = $_POST['Food'];

            if ($food == 'Meat') {

                $tbl .= "<tr><td>{$Animal['meatAnimal']}</td><td>{$Animal['meatHabitat']}</td><td>{$Animal['meatFood']}</td></tr>";

            }
            // Some of you may get confused in how to refer to, say, $classList[3]['Number'].  Note that $classList[3] is an element of the $classList array.  In this foreach loop, each element of the $classList array is represented by $class.  Therefore,  $class['Number'] will represent $classList[0]['number'], $classList[1]['number'], ..., $classList[n]['number'] in turn.
            // Think about how to add to the above condition to make the "ALL" selection works, too.
            else if ($food == 'Grass') {

                $tbl .= "<tr><td>{$Animal['grassAnimal']}</td><td>{$Animal['grassHabitat']}</td><td>{$Animal['grassFood']}</td></tr>";

            }
            else if ($food == 'Mixed') {

                $tbl .= "<tr><td>{$Animal['mixedAnimal']}</td><td>{$Animal['mixedHabitat']}</td><td>{$Animal['mixedFood']}</td></tr>";

            }

        }

    }
    $tbl .= "</table>";
    echo $tbl;
}


// Adding a few links to demo the use of query strings.  Check the URL in your browser for each link and see how query strings are used to pass the prefix information to the script.
?>

这是我要显示的内容的图像:

0 个答案:

没有答案