How do you show an array of associated images

I have a database with a table for property listings (Homes) and another table for the image paths. How do I display only the associated images on a web page when browsing the individual Home listing record. I either get all or just one.

I have attached the database for your review. “postcards.zip” as well as the code below.

[php]

<?php require_once('Connections/postcardsDB.php'); ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $currentPage = $_SERVER["PHP_SELF"]; $maxRows_Listings = 1; $pageNum_Listings = 0; if (isset($_GET['pageNum_Listings'])) { $pageNum_Listings = $_GET['pageNum_Listings']; } $startRow_Listings = $pageNum_Listings * $maxRows_Listings; mysql_select_db($database_postcardsDB, $postcardsDB); $query_Listings = "SELECT listings.property_ID, listings.address1, listings.address2, listings.county, listings.price, listings.`description` FROM listings"; $query_limit_Listings = sprintf("%s LIMIT %d, %d", $query_Listings, $startRow_Listings, $maxRows_Listings); $Listings = mysql_query($query_limit_Listings, $postcardsDB) or die(mysql_error()); $row_Listings = mysql_fetch_assoc($Listings); if (isset($_GET['totalRows_Listings'])) { $totalRows_Listings = $_GET['totalRows_Listings']; } else { $all_Listings = mysql_query($query_Listings); $totalRows_Listings = mysql_num_rows($all_Listings); } $totalPages_Listings = ceil($totalRows_Listings/$maxRows_Listings)-1; $maxRows_AssocImages = 4; $pageNum_AssocImages = 0; if (isset($_GET['pageNum_AssocImages'])) { $pageNum_AssocImages = $_GET['pageNum_AssocImages']; } $startRow_AssocImages = $pageNum_AssocImages * $maxRows_AssocImages; mysql_select_db($database_postcardsDB, $postcardsDB); $query_AssocImages = "SELECT upload_data.property_ID, upload_data.FILE_NAME FROM upload_data ORDER BY upload_data.property_ID"; $query_limit_AssocImages = sprintf("%s LIMIT %d, %d", $query_AssocImages, $startRow_AssocImages, $maxRows_AssocImages); $AssocImages = mysql_query($query_limit_AssocImages, $postcardsDB) or die(mysql_error()); $row_AssocImages = mysql_fetch_assoc($AssocImages); if (isset($_GET['totalRows_AssocImages'])) { $totalRows_AssocImages = $_GET['totalRows_AssocImages']; } else { $all_AssocImages = mysql_query($query_AssocImages); $totalRows_AssocImages = mysql_num_rows($all_AssocImages); } $totalPages_AssocImages = ceil($totalRows_AssocImages/$maxRows_AssocImages)-1; $queryString_Listings = ""; if (!empty($_SERVER['QUERY_STRING'])) { $params = explode("&", $_SERVER['QUERY_STRING']); $newParams = array(); foreach ($params as $param) { if (stristr($param, "pageNum_Listings") == false && stristr($param, "totalRows_Listings") == false) { array_push($newParams, $param); } } if (count($newParams) != 0) { $queryString_Listings = "&" . htmlentities(implode("&", $newParams)); } } $queryString_Listings = sprintf("&totalRows_Listings=%d%s", $totalRows_Listings, $queryString_Listings); ?>

<!doctype html>

Untitled Document

Current Property Listing

<?php do { ?>

Property ID No.: <?php echo $row_Listings['property_ID']; ?>

Street Address: <?php echo $row_Listings['address1']; ?>

City, State, Zipcode<?php echo $row_Listings['address2']; ?>

County: <?php echo $row_Listings['county']; ?>

Description : <?php echo $row_Listings['description']; ?>

 

Select Photos:

<?php } while ($row_Listings = mysql_fetch_assoc($Listings)); ?>

<?php do { ?> <?php } while ($row_AssocImages = mysql_fetch_assoc($AssocImages)); ?>

<?php echo ($startRow_Listings + 1) ?> of <?php echo $totalRows_Listings ?>

Go to "><<Previous Go to ">Next>>

 

 

 

<?php mysql_free_result($Listings);

mysql_free_result($AssocImages);
?>

[/php]


postcards.zip (1.16 KB)

Show_Listings.php.zip (1.74 KB)

Use a MySQL query to select the image associated with a home, something like

[php]“SELECT property_ID,FILE_NAME FROM listings,upload_data WHERE listings.property_ID=upload_data .property_ID AND listings.property_ID=”.$some_home_id[/php]

NOTE: FILE_NAME seems like it’s reserved at least in Notepad++ so it’s best to use some other variable for that just to be on the safe side.

I’m confused. I’ve tried that. All I get is one image. I have at least three images per record. I want to show the 1 main record and the three images on one page.

This query:
[php]SELECT listings.property_ID,upload_data.FILE_NAME FROM listings,upload_data WHERE listings.property_ID=upload_data.property_ID AND listings.property_ID=1[/php]
Provides with the following results:

[table]
[tr]
[td]property_ID[/td]
[td]FILE_NAME[/td]
[/tr]
[tr]
[td]1[/td]
[td]01424981188.jpg[/td]
[/tr]
[tr]
[td]1[/td]
[td]2welch5045b.jpg[/td]
[/tr]
[tr]
[td]1[/td]
[td]1Modern homes designs front views texas…jpg[/td]
[/tr]
[/table]

You need to loop through the results with WHILE or FOREACH

Sponsor our Newsletter | Privacy Policy | Terms of Service