简体   繁体   中英

PHP display table array based off of specific form choice

As the title states I'm trying to have an array table displayed but only displayed those that are based upon a form choice. I have them filtering out but there's still blank rows for the results that aren't being displayed. Any idea how to not display the rows with no results?

Form

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

Here's an image of what I'm getting displayed:

Your code currently tries to access the <$food><Animal/Habitat/Foot> even if the value doesn't exist.

For example if the chosen food is Meat , you're still trying to access $aList[1]['meatAnimal'] , although it doesn't exist.

You should therefore change your code to the following:

// ... previous code
if (isset($_POST['submit'])) {
  $food = strtolower($_POST['Food']);

  if (isset($Animal[$food.'Animal']))
      $tbl .= "<tr><td>{$Animal[$food.'Animal']}</td><td>{$Animal[$food.'Habitat']}</td><td>{$Animal[$food.'Food']}</td></tr>";
}

Note : You should turn on error_reporting - it would've help you in this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM