我有一个产品名称和他们的形象的列表,因为我想避免重复相同的页面对几个产品。我正在做以下几件事:
我正在传递一个变量id,我想要在products.php页面中显示它的名称,因此,例如,我使用products.php?id=anyname并使用$_GET来知道id名称变量。然后,我将使用包含所需所有名称的数组填充菜单。
例如,在我将显示的菜单中:
key: gfon与value: Fondant pt
然后会加载一个带有gfon.png的图像
这是密码
<li>
<a href="menu.html" style="padding:8px 30px;">THE MAIN MENU</a>
<ul>
<?php
if (isset($_GET['id'])) {
$product = $_GET['id'];
}
$array = array(
"gfon" => "Fondant pt",
"galf" => "Alfajores gy",
"gdom" => "Domino tre",
"gesp" => "Espiral ere",
"gsan" => "Sandwich we ",
);
foreach($array as $key => $val) {
echo "<li><a href=\"http://www.mysite.com/products.php?id=".$key."\">".$val."</a> </li>";
}
?>
</ul>
</li>然后是将根据所选择的产品改变图片的部分。
<?php
echo "<h1>";
switch ($product) {
case "gfon":
echo "Fondant</h1>";
break;
case "galf":
echo "Alfajores</h1>";
break;
case "gdom":
echo "Domino</h1>";
break;
case "gesp":
echo "Espiral</h1>";
break;
case "gsan":
echo "Sandwich</h1>";
break;
}
echo "<p> <a href=\"http://www.mysite.com\"><img src=\"images/".$product.".png\" alt=\"" .$product." width=\"300\" height=\"300\" align=\"right\"/> </a>"
?>有时起作用,有时不起作用,有时我会犯这个错误。
内部服务器错误 服务器遇到内部错误或配置错误,无法完成请求。请与服务器管理员联系,通知错误发生的时间和可能导致错误的任何操作。 有关此错误的详细信息可在服务器错误日志中获得。
另外,我没有访问日志文件的权限:(是否有更好的方法来解决这个问题?
发布于 2012-09-18 00:58:19
您确实需要检查服务器日志中的特定问题,但是您的代码也有问题,这里有一些更改。
<?php
// Define your array before checking the $_GET['id']
$array = array(
"gfon" => "Fondant pt",
"galf" => "Alfajores gy",
"gdom" => "Domino tre",
"gesp" => "Espiral ere",
"gsan" => "Sandwich we ",
);
// Check that the id is in the array as a key and assign your product var, else set as null
$product = (isset($_GET['id']) && array_key_exists($_GET['id'],$array)) ? $_GET['id'] : null;
// Output your html
?>
<li>
<a href="menu.html" style="padding:8px 30px;">THE MAIN MENU</a>
<ul>
<?php foreach($array as $key=>$val):?>
<li><a href="http://www.mysite.com/products.php?id=<?=$key?>"><?=$val?></a></li>
<?php endforeach;?>
</ul>
</li>
<?php
// Replacing the switch statement with a simple if else
// Is $product not null? ok it must be within the array
if($product !== null){
// Use explode to chop up the string and grab the first value.
echo '<h1>'.explode(' ',$array[$product])[0].'</h1>';
echo '<p><a href="http://www.mysite.com"><img src="images/'.$product.'.png" alt="'.$product.'" width="300" height="300" align="right" /></a>';
}else{
// Echo something default
echo '<h1>Default</h1>';
echo '<p><a href="http://www.mysite.com"><img src="images/default.png" alt="" width="300" height="300" align="right" /></a>';
}
?>我注意到alt=\"" .$product." width=\"300\"会影响您的输出,因为您没有关闭alt属性。
https://stackoverflow.com/questions/12468801
复制相似问题