Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法获取显示在PHP函数中的结果_Php_Html Table - Fatal编程技术网

无法获取显示在PHP函数中的结果

无法获取显示在PHP函数中的结果,php,html-table,Php,Html Table,我一辈子都搞不明白为什么我的表没有显示结果。它充当值,但不显示文本。我只需要显示基于用户选择的食物类型。 阵列 $aList = array (); $aList[0] = array(); $aList[0]['Animal'] = "Bear"; $aList[0]['Habitat'] = "Forest"; $aList[0]['Food'] = "Meat"; $aList[1] = array(); $aList[1]['Animal'] = "Deer"; $aList[1]

我一辈子都搞不明白为什么我的表没有显示结果。它充当值,但不显示文本。我只需要显示基于用户选择的食物类型。

阵列

$aList = array ();
$aList[0] = array();
$aList[0]['Animal'] = "Bear";
$aList[0]['Habitat'] = "Forest";
$aList[0]['Food'] = "Meat";


$aList[1] = array();
$aList[1]['Animal'] = "Deer";
$aList[1]['Habitat'] = "Forest";
$aList[1]['Food'] = "Grass";

$aList[2] = array();
$aList[2]['Animal'] = "Pig";
$aList[2]['Habitat'] = "Farm";
$aList[2]['Food'] = "Mixed";

$aList[3] = array();
$aList[3]['Animal'] = "Cow";
$aList[3]['Habitat'] = "Farm";
$aList[3]['Food'] = "Grass";

$aList[4] = array();
$aList[4]['Animal'] = "Sheep";
$aList[4]['Habitat'] = "Farm";
$aList[4]['Food'] = "Grass";

$aList[5] = array();
$aList[5]['Animal'] = "Camel";
$aList[5]['Habitat'] = "Desert";
$aList[5]['Food'] = "Grass";

$aList[6]['Animal'] = "Scorpion";
$aList[6]['Habitat'] = "Desert";
$aList[6]['Food'] = "Meat";
函数

function showList($Food){
        // 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>";

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

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

            }

        }
    $tbl .= "</table>";
    echo $tbl;
}
功能列表($Food){
//使用global关键字告诉PHP引擎在函数外部查找变量$classList。否则,PHP引擎将只在函数内部查找$classList。
全球美元汇率;
//设置变量$tbl以存储HTML输出(类列表)
//请注意,标记和表标题行位于foreach循环之外,因此它们不会在循环的每次迭代中重复。
$tbl=“”;
$tbl=$tbl.“动物居住食品”;
foreach($动物){
如果(isset($_POST['submit'])){
$food=strtolower($_POST['food']);
如果(strtolower($Animal['Food'])==$Food)
$tbl.=“{$Animal[$food.'Animal']}{$Animal[$food.'habitation']}{$Animal[$food.'food']}”;
}
如果(isset($_POST['submit'])){
$habitat=strtolower($_POST['habitat']);
if(strtolower($Animal['Habitat'])==$Habitat)
$tbl.=“{$Animal[$habitat.'Animal']}{$Animal[$habitat.'habitat']}{$Animal[$habitat.'Food']}”;
}
}
$tbl.=”;
echo$tbl;
}

您试图在循环中获取
$Animal[$food.Animal']
的值,但在数组中它只是
$Animal['Animal']
。您正在将food变量连接到键,但数组中没有任何其他键,如Animal、habitate和food。因此,您需要从
$Animal
数组中的键值中取出所有$food和$habitat变量。

您实际上不生成任何输出您的函数不回显或返回
$tbl
$food
参数的用途是什么?你没有在函数中的任何地方使用它。对不起,伙计们,我没有复制和粘贴回声…但现在修复了它。我和@Barmar我想我真的不知道这个参数的目的是什么,我可以创建没有参数的函数吗?你到底想用
$Animal[$food.'Animal']
实现什么?您的数组键只是
动物
。没有前缀。这很有效,非常感谢!!!