I have a list of product names and their image, as I want to avoid repeating the same page for several products. I'm doing the following:
I am passing the a variable id
the name I want to appear in the products.php page so for example I am using products.php?id=anyname
and use $_GET
to know the id name variable. Then I am populating a menu with an array wich contains all the names I need
For example the in the menu I will have displayed :
key: gfon
and the value: Fondant pt
and then an image with gfon.png will be loaded
This is the code
<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>
Then comes the section which will change a picture depending on the product chosen
<?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>"
?>
Sometimes it is working and sometimes not, I am getting this error occassionally
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator to inform of the time the error occurred and of anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
Also I do not have access to the log file :( is there any better way to go to solve the this?
You do need to check the server logs for the specific problem but there is also problems with your code here are some changes.
<?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>';
}
?>
I noticed alt=\"" .$product." width=\"300\"
would effect your output as your not closing the alt attribute.