简体   繁体   English

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

[英]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

<?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. 您的代码当前尝试访问<$food><Animal/Habitat/Foot>即使该值不存在。

For example if the chosen food is Meat , you're still trying to access $aList[1]['meatAnimal'] , although it doesn't exist. 例如,如果选择的食物是Meat ,则您仍尝试访问$aList[1]['meatAnimal'] ,尽管它不存在。

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. 注意 :您应该打开error_reporting这样会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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