PDA

View Full Version : Display data from related tables in results


phil.evans372967
04-30-2009, 11:48 AM
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

Ray Borduin
04-30-2009, 12:09 PM
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.

phil.evans372967
04-30-2009, 03:41 PM
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

Ray Borduin
05-01-2009, 04:43 AM
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.

tom92909
05-01-2009, 07:54 AM
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....

phil.evans372967
05-03-2009, 10:08 AM
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);

?>

phil.evans372967
05-04-2009, 10:52 AM
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

Ray Borduin
05-05-2009, 03:16 PM
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.'

phil.evans372967
05-09-2009, 07:34 AM
Thanks Ray

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

Cheers

Phil

phil.evans372967
05-11-2009, 02:58 AM
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>

Ray Borduin
05-11-2009, 08:41 AM
Looks like you are roughly on the right track.

The $_POST hack is like this:

$_POST['myTempVar'] = $row_Recordset1['myRSColumn']

Then you can filter the second recordset with the "POST" value since it won't work filtering from a previous recordset directly.

phil.evans372967
05-11-2009, 11:22 AM
Hi Ray

Genuinely appreciate your time and patience with this. The rest of my app is done, it's just this bit that I'm hitting a wall with.

This is what I've got now, but still can't get the right results

<?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">
<?php $_POST['myTempVar'] = $row_WADAprop_details['prop_id'] ?>
<?php
// Make a MySQL Connection
$query = "SELECT *
FROM prop_images
WHERE prop_id='$myTempVar'
";

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


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

Ray Borduin
05-11-2009, 12:24 PM
looks pretty good. What are the results you are getting? add an echo() to write your SQL statement for your second recordset each time it loops. That might help you spot your problem.

phil.evans372967
05-11-2009, 01:10 PM
My "Thumb" column in my html table shows nothing.
If I remove WHERE prop_id='$myTempVar' then all the images in the database show.
<td class="WADAResultsTableCell">
<?php $_POST['myTempVar'] = $row_WADAprop_details['prop_id'] ?>
<?php
// Make a MySQL Connection
$query = "SELECT *
FROM prop_images
WHERE prop_id='$myTempVar'
";

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


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

Ray Borduin
05-11-2009, 02:40 PM
oh yeah... I assumed that was shorthand for the code you were actually using, which should be:


WHERE prop_id='".$row_WADAprop_details['prop_id']."'";

and since you hand coded your recordset you don't need the $_POST hack, which is only used to make it work with the dw recordset parameters.

phil.evans372967
05-11-2009, 03:02 PM
Ray! Thanks! The scales have been lifted from my eyes and I'm dancing round the office. That did the trick.
Great set of tools and what a great support system you have.
Thanks again.
Regards
Phil

Vinicios Nussemeyer
08-27-2010, 12:04 PM
Hello Phil,

I have a problem like this. You've found the solution to display data from related tables using DataAssist? If you found, you can post your solution here please because there are days I'm trying to do that and I can not.

Thank you for listening,
Vinicios