所以我在编写HTML CSS和PHP时相当新,现在我正在关注PHP。 我们的教授要求我们在我们的网站(没有数据库)中加入PHP代码,并尝试使用for循环和echo打印一个数组。
问题是回声没有显示在网页上,我现在丢失了
下面是我的代码:
<html>
<head>
<title>PHP Loops and Sorts</title>
<body>
<h1>PHP Loops and Sorts</h1>
<div class="container">
<?php
$dogs=Array("Labrador Retriever","German Shepherd","Bulldog","Golden Retriever","Poodle","Beagle","Rottweiler","Yorkshire Terrier","Siberian Husky","Dachshund","Chihuahua","Pug","Great Dane","Dobermann","Shih Tzu");
$cats=Array("Persian","Siamese","Maine Coon","Ragdoll","Sphynx","British Shorthair","Abyssinian","Bengal","Scottish Fold","Himalayan","Russian Blue","Siberian","Munchkin");
$birds=Array("Canaries","Budgies","Finches","Cockatiels","Quaker Parakeets","Pionus Parrots","Poicephalus Parrots","Amazon Parrots","Pyrrhura Conures","Peach-Faced Lovebirds");
$fishes=Array("Koi","Fantail","Oranda","Comet","Black Telescope","Butterfly Tail","Ryukin","Goldfish","Lionhead","Mirror Carp");
function countsize($array,$size){
$size = count($array);
return $size;
}
if(isset($_POST['btnShow']) )
{
$arraypick=$_POST['formAnimal'];
$arrsize = countsize($arraypick,$size);
for(&x = 0,$x<$arrsize,$x++){
echo $arraypick[$x] . "<br>";
}
}
?>
Breeds of different kinds of animals. Select what animal's breed to be shown:<br>
<select name="formAnimal">
<option value="">Choose animal</option>
<option value="dogs">Dog</option>
<option value="cats">Cat</option>
<option value="birds">Bird</option>
<option value="fishes">Fish</option>
</select><br><br>
<div style="margin:auto,text-align:center;text-align:center">
<INPUT TYPE = "Submit" Name = "btnShow" VALUE = "Show List"> 
<INPUT TYPE = "Submit" Name = "btnAsc" VALUE = "Show Ascending"> 
<INPUT TYPE = "Submit" Name = "btnDes" VALUE = "Show Descending">
</div>
<?php
echo $size
?>
</div>
</body>
</html>
答案 0 :(得分:2)
您需要在身体标签前关闭头部标签。
您也不需要将大小传递给方法计数大小。
function countsize (&array) {
return count(&array);
}
答案 1 :(得分:1)
for循环的语法不正确。查看php documentation
此外,$size
变量永远不会在您使用它的范围内声明。
答案 2 :(得分:1)
没有得到你如何使用$ arrsize但你在初始化循环时使用&amp; x而不是$ x。
#include <fstream> // for std::ifstream
#include <string> // for std::string
#include <cstring> // for strcpy()
std::ifstream infile("garbage.txt");
if (infile.is_open())
{
int age;
std::string namestr;
while (infile >> age >> namestr)
{
stud1 temp;
temp.age = age;
strcpy( temp.name, namestr.c_str() );
// do whatever before struct goes out of scope
}
infile.close();
}
答案 3 :(得分:1)
更改以下代码行:
$dogs=Array("Labrador Retriever","German Shepherd","Bulldog","Golden Retriever","Poodle","Beagle","Rottweiler","Yorkshire Terrier","Siberian Husky","Dachshund","Chihuahua","Pug","Great Dane","Dobermann","Shih Tzu");
$cats=Array("Persian","Siamese","Maine Coon","Ragdoll","Sphynx","British Shorthair","Abyssinian","Bengal","Scottish Fold","Himalayan","Russian Blue","Siberian","Munchkin");
$birds=Array("Canaries","Budgies","Finches","Cockatiels","Quaker Parakeets","Pionus Parrots","Poicephalus Parrots","Amazon Parrots","Pyrrhura Conures","Peach-Faced Lovebirds");
$fishes=Array("Koi","Fantail","Oranda","Comet","Black Telescope","Butterfly Tail","Ryukin","Goldfish","Lionhead","Mirror Carp");
function countsize($array,$size){
$size = count($array);
return $size;
}
$arraypick=$_POST['formAnimal'];
$arrsize = countsize($arraypick,$size);
for(&x = 0,$x<$arrsize,$x++){
echo $arraypick[$x] . "<br>";
}
致:
$animalArray['dogs']=Array("Labrador Retriever","German Shepherd","Bulldog","Golden Retriever","Poodle","Beagle","Rottweiler","Yorkshire Terrier","Siberian Husky","Dachshund","Chihuahua","Pug","Great Dane","Dobermann","Shih Tzu");
$animalArray['cats']=Array("Persian","Siamese","Maine Coon","Ragdoll","Sphynx","British Shorthair","Abyssinian","Bengal","Scottish Fold","Himalayan","Russian Blue","Siberian","Munchkin");
$animalArray['birds']=Array("Canaries","Budgies","Finches","Cockatiels","Quaker Parakeets","Pionus Parrots","Poicephalus Parrots","Amazon Parrots","Pyrrhura Conures","Peach-Faced Lovebirds");
$animalArray['fishes']=Array("Koi","Fantail","Oranda","Comet","Black Telescope","Butterfly Tail","Ryukin","Goldfish","Lionhead","Mirror Carp");
function countsize($index){
return count($animalArray[$index]);
}
$arraypick=$_POST['formAnimal'];
$arrsize = countsize($arraypick);
for($x = 0;$x<$arrsize;$x++){
echo $animalArray[$arraypick][$x] . "<br>";
}
答案 4 :(得分:1)
if(isset($_POST['btnShow']) )
你确定它已经确定了吗?
根据我们的代码,你甚至不能POST
,你错过了form
标签
尝试用以下内容包装输入:
<form method="POST">
<!--inputs-->
</form>