close ad
WARNING PC USERS: Do Not Install the DREAMWEAVER CC 2017 Update »
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Display data from related tables in results

Thread began 4/30/2009 2:48 pm by phil.evans372967 | Last modified 8/27/2010 3:04 pm by Vinicios Nussemeyer | 5107 views | 16 replies |

phil.evans372967

Display data from related tables in results

Hi all,

I'm trying to display data in my main results pages from two related tables

prop_details
prop_images

I'd like display all thumbnails from prop_images that are related to prop_details, the relationship being prop_id

I've create a new recordset



<?php
$maxRows_WADAprop_details = 5;
$pageNum_WADAprop_details = 0;
if (isset($_GET['pageNum_WADAprop_details'])) {
$pageNum_WADAprop_details = $_GET['pageNum_WADAprop_details'];
}
$startRow_WADAprop_details = $pageNum_WADAprop_details * $maxRows_WADAprop_details;

mysql_select_db($database_prop, $prop);
$query_WADAprop_details = "SELECT prop_ref, prop_id, add_date, prop_type, short_desc, town, price, status, featured FROM prop_details";
$query_limit_WADAprop_details = sprintf("%s LIMIT %d, %d", $query_WADAprop_details, $startRow_WADAprop_details, $maxRows_WADAprop_details);
$WADAprop_details = mysql_query($query_limit_WADAprop_details, $prop) or die(mysql_error());
$row_WADAprop_details = mysql_fetch_assoc($WADAprop_details);

if (isset($_GET['totalRows_WADAprop_details'])) {
$totalRows_WADAprop_details = $_GET['totalRows_WADAprop_details'];
} else {
$all_WADAprop_details = mysql_query($query_WADAprop_details);
$totalRows_WADAprop_details = mysql_num_rows($all_WADAprop_details);
}
$totalPages_WADAprop_details = ceil($totalRows_WADAprop_details/$maxRows_WADAprop_details)-1;

mysql_select_db($database_prop, $prop);
$query_SelectThumb = "SELECT prop_images.img_thumb, prop_images.prop_id, prop_details.prop_id FROM prop_images, prop_details WHERE prop_images.prop_id = prop_details.prop_id";
$SelectThumb = mysql_query($query_SelectThumb, $prop) or die(mysql_error());
$row_SelectThumb = mysql_fetch_assoc($SelectThumb);
$totalRows_SelectThumb = mysql_num_rows($SelectThumb);
?>

which makes the match (I think)

but I can't get the image file names to display in my table row in the main repeat region

<?php do { ?>
<tr class="<?php echo $WARRT_AltClass1->getClass(true); ?>">
<td class="WADAResultsTableCell"><?php echo($row_WADAprop_details['prop_ref']); ?></td>
<td class="WADAResultsTableCell"><?php echo($row_WADAprop_details['prop_id']); ?></td>
<td class="WADAResultsTableCell"><?php echo($row_WADAprop_details['add_date']); ?></td>
<td class="WADAResultsTableCell">Display all image in series related to the record here<?php echo $row_SelectThumb['img_thumb']; ?></td>
<td class="WADAResultsTableCell"><?php echo($row_WADAprop_details['prop_type']); ?></td>
<td class="WADAResultsTableCell"><?php echo($row_WADAprop_details['short_desc']); ?></td>
<td class="WADAResultsTableCell"><?php echo($row_WADAprop_details['town']); ?></td>
<td class="WADAResultsTableCell"><?php echo($row_WADAprop_details['price']); ?></td>
<td class="WADAResultsTableCell"><?php echo($row_WADAprop_details['status']); ?></td>
<td class="WADAResultsTableCell"><?php echo($row_WADAprop_details['featured']); ?></td>
</tr>
<?php } while ($row_WADAprop_details = mysql_fetch_assoc($WADAprop_details)); ?>

Can anyone help?

Thanks

Phil

Sign in to reply to this post

Ray BorduinWebAssist

You are going to have to move your thumbs recordset inside your details loop and you are going to have to filter it based on the detail being displayed... then you are going to have to add a repeat region around the thumbs display line show it displays for as many thumbs as exist for that detail.

Sign in to reply to this post

phil.evans372967

Hi Ray,

Thanks for your reply

I'm not sure what with the first part. Do you mean combine the two queries into one something like this

SELECT prop_details.prop_id, prop_details.prop_type, prop_details.short_desc, prop_details.prop_ref, prop_images.img_thumb, prop_images.prop_ref, prop_images.prop_id
FROM prop_details, prop_images
WHERE prop_images.prop_id=prop_details.prop_id

Thanks

Phil

Sign in to reply to this post

Ray BorduinWebAssist

I mean cutting and pasting

You need to add a parameter to the second recordset to filter based on the first recordset. To do this you need to do the $_POST hack and set it to a posted variable and set the post variable to the recordset value manually.

Then you need to cut and paste the recordset code inside the DO loop on the page so that it is refiltered for each result.

Another possible technique is to use a joined recordset and write if statments and loops, but that takes even more hand coding.

Sign in to reply to this post

tom92909Beta Tester

Using an INNER JOIN would be good.

"SELECT *
FROM prop_details INNER JOIN prop_images ON prop_images.prop_id = prop_details.prop_id"

Something like that should help I think....

Sign in to reply to this post

phil.evans372967

Hi guys

Thanks for your help with this.

So far I've managed to get the page to display all the related records, but the script displays a row for each prop_id from prop_details that has a related img_thumb.

Any idea how I can get this to display just one row for each record for prop_id in prop_details and then display all related img_thumbs in one cell in the table (the one that currently shows <td><img src="../images/tns/<?php echo $row_Recordset1['img_thumb']; ?>" width="50" height="50" alt="<?php echo $row_Recordset1['short_desc']; ?>" /></td>)

Thanks again

Phil

<?php require_once('../Connections/prop.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$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;
}
}

mysql_select_db($database_prop, $prop);
$query_Recordset1 = "SELECT prop_details.prop_id, prop_details.prop_ref, prop_details.short_desc, prop_images.img_thumb, prop_images.prop_id FROM prop_details LEFT JOIN prop_images ON prop_details.prop_id=prop_images.prop_id";
$Recordset1 = mysql_query($query_Recordset1, $prop) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<table width="80%" border="0" cellspacing="2" cellpadding="2">
<tr>
<td>Prop ID</td>
<td>Office Ref</td>
<td>Short Desc</td>
<td>Thumbs</td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_Recordset1['prop_id']; ?></td>
<td><?php echo $row_Recordset1['prop_ref']; ?></td>
<td><?php echo $row_Recordset1['short_desc']; ?></td>
<td><img src="../images/tns/<?php echo $row_Recordset1['img_thumb']; ?>" width="50" height="50" alt="<?php echo $row_Recordset1['short_desc']; ?>" /></td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>
</body>
</html>
<?php
mysql_free_result($Recordset1);

?>

Sign in to reply to this post

phil.evans372967

I've adapted my query further which now pulls out all the right results, but only displays the first image associated with the property record

SELECT prop_details.prop_id, prop_details.prop_ref, prop_details.short_desc, prop_images.img_thumb, prop_images.prop_id, prop_details.long_desc
FROM prop_details LEFT JOIN prop_images ON prop_details.prop_id=prop_images.prop_id
GROUP BY prop_images.prop_id, prop_details.prop_id
ORDER BY prop_details.prop_ref

Can anyone guide me as to how to adapt my PHP block to acheive this?

This is what I've got so far

<body>
<table width="80%" border="0" cellspacing="2" cellpadding="2">
<tr>
<td>Prop ID From Ims Table</td>
<td>Prop ID from Deatils table</td>
<td>Office Ref</td>
<td>Short Desc</td>
<td>Long Desc</td>
<td>Thumbs</td>
</tr>
<?php do { ?>
<tr>
<td align="left" valign="top"><?php echo $row_Recordset1['prop_id']; ?></td>
<td align="left" valign="top"><?php echo $row_Recordset1['prop_id']; ?></td>
<td align="left" valign="top"><?php echo $row_Recordset1['prop_ref']; ?></td>
<td align="left" valign="top"><?php echo $row_Recordset1['short_desc']; ?></td>
<td align="left" valign="top"><?php echo $row_Recordset1['long_desc']; ?></td>
<td align="left" valign="top"><img src="../images/tns/<?php echo $row_Recordset1['img_thumb']; ?>" width="50" height="50" alt="" /></td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

With thanks as ever

Phil

Sign in to reply to this post

Ray BorduinWebAssist

Just listen to my original advice... well first you will need to add a second recordset then:

'You need to add a parameter to the second recordset to filter based on the first recordset. To do this you need to do the $_POST hack and set it to a posted variable and set the post variable to the recordset value manually.

Then you need to cut and paste the recordset code inside the DO loop on the page so that it is refiltered for each result.'

Sign in to reply to this post

phil.evans372967

Thanks Ray

How do I do the $_POST hack and set it to a posted variable?

Cheers

Phil

Sign in to reply to this post

phil.evans372967

Am I on the right path with this

<?php do { ?>
<tr class="<?php echo $WARRT_AltClass1->getClass(true); ?>">
<td class="WADAResultsTableCell"><?php echo($row_WADAprop_details['prop_id']); ?></td>
<td class="WADAResultsTableCell"><?php echo($row_WADAprop_details['add_date']); ?></td>
<td class="WADAResultsTableCell"><?php echo($row_WADAprop_details['prop_type']); ?></td>
<td class="WADAResultsTableCell"><?php echo($row_WADAprop_details['short_desc']); ?></td>
<td class="WADAResultsTableCell"><?php echo($row_WADAprop_details['town']); ?></td>
<td class="WADAResultsTableCell"><?php echo($row_WADAprop_details['price']); ?></td>
<td class="WADAResultsTableCell"><?php echo($row_WADAprop_details['prop_ref']); ?></td>
<td class="WADAResultsTableCell"><?php echo($row_WADAprop_details['status']); ?></td>
<td class="WADAResultsTableCell">
<form action="" method="post"><input name="$prop_id" type="hidden" id="$prop_id" value="<?php echo $row_WADAprop_details['prop_id']; ?>" />
</form>
<?php
// Make a MySQL Connection
$query = "SELECT *
FROM prop_images
WHERE prop_id = '$prop_id'
";

$result = mysql_query($query) or die(mysql_error());


while($row = mysql_fetch_array($result)){
echo $row['img_thumb'];
echo "<br />";
}
?>
</td>

Sign in to reply to this post
loading

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...