PHP单选按钮,多维数组
Posted
技术标签:
【中文标题】PHP单选按钮,多维数组【英文标题】:PHP radio buttons, multidimensional array 【发布时间】:2014-11-11 00:11:49 【问题描述】:好吧,我不知道我在做什么。我认为我已经正确设置了我的多维数组。我遇到的问题是当您选择单选按钮时,结果需要出来。我是这个 php 东西的完全菜鸟....
这是我的单选按钮 html:
<h2>List animals by </h2>
<ul>
<li>Habitat: (This selection doesn't work in the demonstration)
<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="Food" value="Meat" ?prefix=Meat> Meat
<input type="radio" name="Food" value="Grass" ?prefix=Grass> Grass
<input type="radio" name="Food" value="Mixed" ?prefix=Mixed> Mixed
<input type="submit" name="submit" value="List Animals">
</form>
</ul>
<hr>
这是我的 php .....我完全是个菜鸟。这是我的第一个“任务”。我在这方面有太多的麻烦。一些能指引我正确方向的指针会很棒。
<?php
if (isset($_post['submit']))
if (isset($_post['radio']))
$animalList = array ();
$animalList[0] = array ();
$animalList[0] ['Animal'] = "Bear";
$animalList[0] ['Habitat'] = "Forest";
$animalList[0] ['Food'] = "Meat";
$animalList[1] = array();
$animalList[1] ['Animal'] = "Deer";
$animalList[1] ['Habitat'] = "Forest";
$animalList[1] ['Food'] = "Grass";
$animalList[2] = array();
$animalList[2] ['Animal'] = "Pig";
$animalList[2] ['Habitat'] = "Farm";
$animalList[2] ['Food'] = "Mixed";
$animalList[3] = array();
$animalList[3] ['Animal'] = "Cow";
$animalList[3] ['Habitat'] = "Farm";
$animalList[3] ['Food'] = "Grass";
$animalList[4] = array();
$animalList[4] ['Animal'] = "Sheep";
$animalList[4] ['Habitat'] = "Farm";
$animalList[4] ['Food'] = "Grass";
$animalList[5] = array();
$animalList[5] ['Animal'] = "Camal";
$animalList[5] ['Habitat'] = "Desert";
$animalList[5] ['Food'] = "Grass";
$animalList[6] = array();
$animalList[6] ['Animal'] = "Scorpion";
$animalList[6] ['Habitat'] = "Desert";
$animalList[6] ['Food'] = "Meat";
function showAnimals($prefix_requested)
global $animalList;
$tbl = "<table border=1>";
$tbl = $tbl."<tr><th>Animal</th><th>Habitat</th><th>Food</th></tr>";
foreach ($animalList as $animal)
if ($animal['Animal'] == $prefix_requested)
$tbl .= "<tr><td>$animal['Animal']
$animal['Habitat']</td><td>
$animal['Food']</td></tr>";
$tbl .="</table>";
echo $tbl;
echo "".$_post['radio'];
?>
有点长,我知道...如果有人能以正确的方式引导我,我将不胜感激!!!!!!
【问题讨论】:
我建议为此使用数据库 【参考方案1】:<?php
function showAnimals($seloption, $prefixsel)
$animalList = array ();
$animalList[0] ['Animal'] = "Bear";
$animalList[0] ['Habitat'] = "Forest";
$animalList[0] ['Food'] = "Meat";
$animalList[1] ['Animal'] = "Deer";
$animalList[1] ['Habitat'] = "Forest";
$animalList[1] ['Food'] = "Grass";
$animalList[2] ['Animal'] = "Pig";
$animalList[2] ['Habitat'] = "Farm";
$animalList[2] ['Food'] = "Mixed";
$animalList[3] ['Animal'] = "Cow";
$animalList[3] ['Habitat'] = "Farm";
$animalList[3] ['Food'] = "Grass";
$animalList[4] ['Animal'] = "Sheep";
$animalList[4] ['Habitat'] = "Farm";
$animalList[4] ['Food'] = "Grass";
$animalList[5] ['Animal'] = "Camal";
$animalList[5] ['Habitat'] = "Desert";
$animalList[5] ['Food'] = "Grass";
$animalList[6] ['Animal'] = "Scorpion";
$animalList[6] ['Habitat'] = "Desert";
$animalList[6] ['Food'] = "Meat";
$tbl = "<table border=1>";
$tbl = $tbl."<tr><th>Animal</th><th>Habitat</th><th>Food</th></tr>";
foreach ($animalList as $animal)
if ($animal[$prefixsel] == $seloption)
$tbl .= "<tr><td>".$animal['Animal']."</td><td>".$animal['Habitat']."</td><td>".$animal['Food']."</td></tr>";
$tbl .="</table>";
echo $tbl;
if (isset($_POST['submit1']))
showAnimals($_POST['Habitat'], 'Habitat');
if (isset($_POST['submit2']))
showAnimals($_POST['Food'], 'Food');
?>
<h2>List animals by </h2>
<ul>
<li>Habitat: (This selection doesn't work in the demonstration)
<form action="" method="post">
<input type="radio" name="Habitat" value="Forest" <?php echo ($_POST['Habitat']=='Forest'?'checked="checked"':'');?>> Forest
<input type="radio" name="Habitat" value="Farm" <?php echo ($_POST['Habitat']=='Farm'?'checked="checked"':'');?>> Farm
<input type="radio" name="Habitat" value="Desert" <?php echo ($_POST['Habitat']=='Desert'?'checked="checked"':'');?>> Desert
<input type="submit" name="submit1" value="List Animals">
</form>
<li>
Food:
<form action="" method="post">
<input type="radio" name="Food" value="Meat" <?php echo ($_POST['Food']=='Meat'?'checked="checked"':'');?>> Meat
<input type="radio" name="Food" value="Grass" <?php echo ($_POST['Food']=='Grass'?'checked="checked"':'');?>> Grass
<input type="radio" name="Food" value="Mixed" <?php echo ($_POST['Food']=='Mixed'?'checked="checked"':'');?>> Mixed
<input type="submit" name="submit2" value="List Animals">
</form>
</ul>
<hr>
【讨论】:
谢谢您,我下班后会检查一下,看看情况如何 哇,谢谢,效果很好。会投票,但我没有足够的声誉!再次感谢您。以上是关于PHP单选按钮,多维数组的主要内容,如果未能解决你的问题,请参考以下文章