Any help is greatly appreciated. I’m new to PHP and MySQL. My PRODUCTS.PHP file has dynamic elements by assigning variables from a MySQL query. I have a link from the CATALOG.PHP page to PRODUCTS.PHP?ID=$ID where $ID is the primary key for a product. I then assign 4 other variables for the 2 next and 2 previous items in the database. I show all 5 thumnails at the top of the page arranged like this:
$imageID1-$imageID2-$imageID3(current product)-$imageID4-$imageID5
This allows the user to get to view a larger picture of the next item in the catalog(table) without having to go back to the catalog page to select it. They can just keep clicking on the next button or on the thumbnail to jump to one of the detail pages.
Where I run into a problem is when $ID=1 there is a negative value for the first two images ($imageID1, $imageID2) and when $ID=2 there is a negative value for the first image ($imageID1). When the current product is the last product in the table there should not be a value for ($imageID4, $imageID5). How do I get the PHP code ignore those image fields if there is no record to display? I thought of making the query die if the array returned no values. Where can I put an if statement to make sure I don’t get an image reference in that section? Maybe if it just displays a default image if none is found in the database.
Please help!
Here is the PRODUCTS.PHP file:
[php]<?php
//Connection to database
require_once(’…/…/Connections/db_config.php’);
//Set ID’s to query database and set variables
if(isset($_GET[‘ID’])) {
$ID = htmlspecialchars($_GET[‘ID’]);
$image1ID = $ID - 2;
$image2ID = $ID - 1;
$image4ID = $ID + 1;
$image5ID = $ID + 2;
//Retrieve records from database
$sql =“SELECT * FROM PRODUCTS WHERE ID=’$ID’”;
$result = mysql_query($sql, $connection);
$sql1 = “SELECT * FROM PRODUCTS WHERE ID=’$image1ID’”;
$result1 = mysql_query($sql1, $connection);
$sql2 = “SELECT * FROM PRODUCTS WHERE ID=’$image2ID’”;
$result2 = mysql_query($sql2, $connection);
$sql4 = “SELECT * FROM PRODUCTS WHERE ID=’$image4ID’”;
$result4 = mysql_query($sql4, $connection);
$sql5 = “SELECT * FROM PRODUCTS WHERE ID=’$image5ID’”;
$result5 = mysql_query($sql5, $connection);
}
//Set main product variables
$row = mysql_fetch_array($result);
$prodid = $row[‘PRODID’];
$desc = $row[‘DESCRIPTION’];
$bride = $row[‘BRIDE’];
$groom = $row[‘GROOM’];
$URL = $row[‘URL’];
$thumbURL = $row[‘THUMBURL’];
$photoURL = $row[‘PHOTOURL’];
$thumb = $row[‘THUMB’];
$photo = $row[‘PHOTO’];
$negthumb = $row[‘NEGTHUMB’];
$watermark = $row[‘WATERMARK’];
//Set other product variables
$row1 = mysql_fetch_array($result1);
$desc1 = $row1[‘DESCRIPTION’];
$thumb1 = $row1[‘THUMB’];
$negthumb1 = $row1[‘NEGTHUMB’];
$row2 = mysql_fetch_array($result2);
$desc2 = $row2[‘DESCRIPTION’];
$thumb2 = $row2[‘THUMB’];
$negthumb2 = $row2[‘NEGTHUMB’];
$row4 = mysql_fetch_array($result4);
$desc4 = $row4[‘DESCRIPTION’];
$thumb4 = $row4[‘THUMB’];
$negthumb4 = $row4[‘NEGTHUMB’];
$row5 = mysql_fetch_array($result5);
$desc5 = $row5[‘DESCRIPTION’];
$thumb5 = $row5[‘THUMB’];
$negthumb5 = $row5[‘NEGTHUMB’];
?>