[英]Img won't display using PHP
该代码的要旨是使用post变量显示n次动物图片。
问题在于,仅alt显示了图像。
这是完整的代码:给定URL:/index.php?animal=boxer&count=3
<?php
$animal = $_GET['animal'];
$count = $_GET['count'];
$valid_animals = array("crow", "boxer", "rat");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Animoz</title>
</head>
<body>
<p>
<?php if (in_array($animal, $valid_animals)): ?>
<?php for ($j = 0; $j < $count; $j++): ?>
<img src="images/<?=$animal?>.jpg" alt="<?=$animal?>">
<?php endfor; ?>
<?php else: ?>
<p> I can't show you any <?=$animal?>s!!!!</p>
<?php endif; ?>
</p>
</body>
</html>
我也尝试过:
<?php
if (in_array($animal, $valid_animals))
{
for ($j = 0; $j < $count; $j++)
{
echo '<img src="'. $animal . '.jpg" alt="' . $animal . '">';
}
}
?>
它附带的HTML代码看起来不错:
<body>
<p>
<img src="images/boxer.jpg" alt="boxer">
<img src="images/boxer.jpg" alt="boxer">
<img src="images/boxer.jpg" alt="boxer">
</p>
</body>
使用的文件路径正确。
知道我在这里做错了什么吗?
图像是从/Part2.php/images而不是/ Part2 / images(Part2也包含Part2.php)中提取的。 显然,仅使用“ src = / images”是不够的–我最终不得不使用“ src = .. / images”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.