Displaying images on Product Detail page from database


#1

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’];
?>

Untitled Document
">" alt="<?php echo "{$desc1}"; ?>" width="120" height="90" border="0"> ">" alt="<?php echo "{$desc2}"; ?>" width="120" height="90"> ">" alt="<?php echo "{$desc}"; ?>" width="120" height="90"> ">" alt="<?php echo "{$desc4}"; ?>" width="120" height="90"> ">" alt="<?php echo "{$desc5}"; ?>" width="120" height="90">
[/php]

#2

If I understand you correctly, this might be the perfect opportunity to use a SWITCH.

[php]
SWITCH ($id) {

case 1 :
// echo code leaving out first two images
break;
case 2:
// echo code leaving out first image
break;
case n-2:
// echo code leaving out last image
break;
case n-1;
// echo code leaving out last two images
break;
default:
//echo code showing all 5 images
}
[/php]

Now from what you describe you would need to leave off the first one or two images depending on the ID number. Presumably you don’t have an infinite number of images either so you would need to do the same on the other end. I represent it by n-1 and n-2. You could actually use a variiable or the actual number.

Finally, you could also just loop back around the end, when ID = 1 then the image # -2 is the Max number -1 etc…

I hope I made sense of this for you (or anyone else reading it.)


#3

Thank you so much! It works perfectly now. There is only one thing (very minor). I am looking at the source code of the page and all of the echo’ed lines are on the same line. It’s not a big deal to me, but I am a little anal when it comes to the code looking uniform. When I was writing it I found it hard to troubleshoot, because everything was bunched up. Any ideas?

Here is the source from the browser:

MOD EDIT: Corrected mis-typed PHP bb code tags
[php]

Untitled Document
       
[/php] Not very pretty.

here is the code:

[php] <?php require_once(’…/…/Connections/db_config.php’);

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);
$URL = $row[‘URL’];
$thumbURL = $row[‘THUMBURL’];
$thumb = $row[‘THUMB’];
//Set other product variables
$row1 = mysql_fetch_array($result1);
$desc1 = $row1[‘DESCRIPTION’];
$thumb1 = $row1[‘THUMB’];

$row2 = mysql_fetch_array($result2);
$desc2 = $row2[‘DESCRIPTION’];
$thumb2 = $row2[‘THUMB’];

$row4 = mysql_fetch_array($result4);
$desc4 = $row4[‘DESCRIPTION’];
$thumb4 = $row4[‘THUMB’];

$row5 = mysql_fetch_array($result5);
$desc5 = $row5[‘DESCRIPTION’];
$thumb5 = $row5[‘THUMB’];
?>

Untitled Document <?php //Enter switch() information here switch ($id): case 1 : // echo code leaving out first two images echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; break; case 2: // echo code leaving out first image echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; break; case n-2: // echo code leaving out last image echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; break; case n-1; // echo code leaving out last two images echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; break; default: //echo code showing all 5 images echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; endswitch; ?>
                          
[/php]

Anyway, thanks again!!!


#4

On lines that you echo you can add a rn to the string. It has no effect on the displayed page, but the underlying code it will add carriage returns ® and a Newline (n).


#5

Thank you very much! Everything is perfect now.