close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

combining 2 tables using a 3rd table as the link table.

Thread began 7/28/2014 6:44 am by Christopher West | Last modified 8/01/2014 11:23 am by Jason Byrnes | 2198 views | 13 replies |

Christopher WestCommunity Expert

combining 2 tables using a 3rd table as the link table.

Hi there, I have a right hand side section that will display a selection of products and feedback (see the attached screenshot) - Currently its working since I am using 2 recordsets (one for the products and one for the feedback) but since I am using this approach it means I cannot mix up the products together with the feedback (with 2 seperate recordsets it would display all products first and then display all feedback directly under).

both the recordsets are using a link table (called featured) see attached screenshot) - The featured table uses FeatureType to determine 1 if its a product and 2 if its feedback) (currently im not using Featured Position). And FeatureTypeID is either the ID for Products table or Feedback table.

im also using a variable to store the server request URL so that I can then pull in different productd and feedback depending on the page.

heres my current recordset:

$basefile = basename ( $_SERVER["REQUEST_URI"] );

mysql_select_db($database_dinkydb, $dinkydb);
$query_rsFeaturedProducts = "SELECT * FROM featured INNER JOIN products ON featured.FeaturedTypeID = products.ProductID WHERE FeaturedLocale = '$basefile'";
$rsFeaturedProducts = mysql_query($query_rsFeaturedProducts, $dinkydb) or die(mysql_error());
$row_rsFeaturedProducts = mysql_fetch_assoc($rsFeaturedProducts);
$totalRows_rsFeaturedProducts = mysql_num_rows($rsFeaturedProducts);

mysql_select_db($database_dinkydb, $dinkydb);
$query_rsFeaturedFeedback = "SELECT * FROM featured INNER JOIN feedback ON featured.FeaturedTypeID = feedback.FeedbackID WHERE FeaturedLocale = '$basefile'";
$rsFeaturedFeedback = mysql_query($query_rsFeaturedFeedback, $dinkydb) or die(mysql_error());
$row_rsFeaturedFeedback = mysql_fetch_assoc($rsFeaturedFeedback);
$totalRows_rsFeaturedFeedback = mysql_num_rows($rsFeaturedFeedback);



and here is my actual php code:

php:
<?php if ($totalRows_rsFeaturedProducts 0) { // Show if recordset not empty ?>

<div class="clearfix"></div>
      <div class="feature-heading">Featured Product</div>
      <?php do { ?>
      
      <p class="feature-name"><?php echo $row_rsFeaturedProducts['ProductName']; ?></p>
      <div class="clearfix"></div>
      <p class="feature-price">From only £<?php echo $row_rsFeaturedProducts['ProductPrice']; ?></p>
      <p class="feature-image"><a href="../product.php?Product=<?php echo $row_rsFeaturedProducts['ProductLink']; ?>"><img  src="../images/products/large/<?php echo $row_rsFeaturedProducts['ProductImage']; ?>" width="250" alt="<?php echo $row_rsFeaturedProducts['ProductImageTitle']; ?>"/></a></p>
      <?php } while ($row_rsFeaturedProducts mysql_fetch_assoc($rsFeaturedProducts)); ?>
  <?php // Show if recordset not empty ?>
<?php 
if ($totalRows_rsFeaturedFeedback 0) { // Show if recordset not empty ?>
<div class="feedback-heading">What our Customers say!</div>
<div class="clearfix"></div>
<p class="feedback-other"><a href="../what-our-customers-say.php">Read other Customer Reviews</a></p>
  <?php do { ?>
      <div class="clearfix"></div>
      <p class="feedback-quote"><?php echo $row_rsFeaturedFeedback['FeedbackText']; ?></p>
      <p class="feedback-name"><strong><?php echo $row_rsFeaturedFeedback['FeedbackSalutation']; ?> <?php echo $row_rsFeaturedFeedback['FeedbackFirstName']; ?> <?php echo $row_rsFeaturedFeedback['FeedbackLastName']; ?> | <?php echo $row_rsFeaturedFeedback['FeedbackLocation']; ?></strong></p>
      <p class="feedback-date"><?php echo date('jS M Y',strtotime($row_rsFeaturedFeedback['FeedbackDate'])); ?></p>
    <?php } while ($row_rsFeaturedFeedback mysql_fetch_assoc($rsFeaturedFeedback)); ?>
  <?php // Show if recordset not empty ?>



So what I want to do is create only ONE recordset and only ONE section of PHP code so that it either displays products or feedback. Obviously I can write the php code to display either products or feedback.

But Im having trouble created the ONE recordset so that it links products, feedback using the featured table.

I tried different joins etc but no luck,

someone told me I me I should use sub-queries to do what I require, but not really used sub-queries before.

So my question is how do I construct my recordset set to use Products, Feedback and Featured Tables so I can then display both products and feedback in a completely random way on my page (so for example down the right column of website the order could be something like this (Product ; Product ; Feedback ; Product ; Feedback; Feedback ; Product ; Feedback ; Product) (rather then display all products first and then all feedback under which is currently what my page is doing).

Chris

Sign in to reply to this post

Jason ByrnesWebAssist

use inner joins to join all 3 tables together


SELECT *
FROM products
INNER JOIN featured ON = products.ProductID = featured.FeaturedTypeID
INNER JOIN feedback ON feedback.FeedbackID = featured.FeaturedTypeID

NOTE:

INNER JOIN requires that there be a matching record on both sides to return a result, you may need to use another join type like LEFT OUTER JOIN

see this page for more details on the other join types:
http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/

Sign in to reply to this post

Christopher WestCommunity Expert

Hi Jason, hmm I see your point about that inner join needs both records matching...it seems that left outer join doesnt work either.

I was wondering in your SQL your using FROM Products first...(but since that products and feedback are both equals in this scenerio wouldnt I need to start with SELECT featured (and then use a join for both products and feedback tables to connect to featured table).

I have previously tried different versions of SQL but without luck.

this is currently my SQL

php:
$basefile = basename ( $_SERVER["REQUEST_URI"] );


mysql_select_db($database_dinkydb, $dinkydb);
$query_rsFeatured = "SELECT * FROM products LEFT OUTER JOIN featured ON products.ProductID = featured.FeaturedTypeID LEFT OUTER JOIN feedback ON feedback.FeedbackID = featured.FeaturedTypeID WHERE FeaturedLocale = '$basefile'";
$rsFeatured = mysql_query($query_rsFeatured, $dinkydb) or die(mysql_error());
$row_rsFeatured = mysql_fetch_assoc($rsFeatured);
$totalRows_rsFeatured = mysql_num_rows($rsFeatured);



As stated before I was told the only way to solve this is to use sub-queries - but i havent had much expeirence in this.

Side Note: to be clear the 2 tables Products and Feedback have no relationship with each other. All im doing is wanting to mix up the displaying of random products and random feedback (which IDs are are stored in the Featured table) so the only way to do this is to stored product ids and feedback ids in one single table so thay when i do a recordset repeat region i can have them displaying randomly.

I was told that: Sub queries can only bring back 1 row on the sub query but are great for calculating a total.

A sub query on a join will behave like a union query but the fields don't have to match. It will return you all the rows from both tables.

The alternative is to have one field brought back as an array of all records as a coma separated list and simply loop over that list.

So I think the way to solve this issue is to use a sub query, but in context to my tables as noted above I dont know how.

Any other ideas?
Chris

Sign in to reply to this post

Jason ByrnesWebAssist

can you give the table structure for all 3 tables, it seams like you aren't doing the join on the correct column.

The join should be done on the linking columns in the featured table


  As stated before I was told the only way to solve this is to use sub-queries - but i havent had much expeirence in this.  



this tutorial should help with nested recordsets:

http://www.webassist.com/tutorials/Display-nested-recordsets

Sign in to reply to this post

Christopher WestCommunity Expert

I hada quick look at that tutorial, most of it was refering how to build the page etc. but all i need is the actual recordset code. What I was thinking though when you sent me that link, isnt a nested recordset code that gets placed inside a repeat region...if thats correct then I dont think that would work in my case. Im wondering if I have explained my issues completely wrong and you getting the wrong idea...unless I am missing it completely :)

I was giving this code example which I need to adjust for my own purpose, but this was using sub-query:

php:
Select products.product_id, products.product_name, othertable.category_id, othertable.category_name

from products
JOIN (SELECT categories.category_id, categories.category_name) othertable ON (products.category_id = other table.category_id)



But then again when looking at that code, I dont see how that would work to fit my purpose.

Its so frustrating that I cannot understand it. I need a beer I think!

from that link tutorial you gave me, though I cannot see how that recordset example would help me.

Chris

When I have tried various examples of recordsets, as each page content will be different, I got one page that displays ALL products (which is fine as thats what I added into the featured table), however another page that may have a mix od products and feedback it displays the product, however below it it doesnt display the feedback code, yet it displays the layout of the product (ie missing image and missing heading, so its assumning its a product but since there is no ID associated with it then it wont display the data). and thn on another page where its suppose to display a few feedback entries it only displays 1 and the second comes with a warning "Warning: mysql_fetch_assoc() expects parameter 1 to be resource, array given in C:\xampp\htdocs\dinky\plugins\plg-side-information.php on line 106"

Im going crazy lol

Sign in to reply to this post

Jason ByrnesWebAssist

I cant really offer any more info without the table structure.

I think you are using the wrong columns for linking, but i need the table structure for more details.

Sign in to reply to this post

Christopher WestCommunity Expert

ok Im attaching the 3 tables as images and I am attaching a crude example I put together in photoshop (however because of the featured table it will allow me to control in which order the products and feedback are displayed for each individual page).

plus i am copying the current code to display the actual repeat region to give it context.

php:
<?php

  
if ("".$row_rsFeatured['FeaturedType']  ."" == "1") {  // WebAssist Show If
?>
        <div class="feature-heading">Featured Product</div>
        <?php do { ?>
          <p class="feature-name"><?php echo $row_rsFeatured['ProductName']; ?></p>
          <div class="clearfix"></div>
          <p class="feature-price">From only £<?php echo $row_rsFeatured['ProductPrice']; ?></p>
          <p class="feature-image"><a href="../product.php?Product=<?php echo $row_rsFeatured['ProductLink']; ?>"><img  src="../images/products/large/<?php echo $row_rsFeatured['ProductImage']; ?>" width="250" alt="<?php echo $row_rsFeatured['ProductImageTitle']; ?>"/></a></p>
          <?php } while ($row_rsFeatured mysql_fetch_assoc($rsFeatured)); ?>
        <?php
  
// ("".$row_rsFeatured['FeaturedType']  ."" == "1")
?>
      <div class="clearfix"></div>
      
      <?php
  
if ("".$row_rsFeatured['FeaturedType']  ."" == "2") {  // WebAssist Show If
?>
  <div class="feedback-heading">What our Customers say!</div>
  <p class="feedback-other"><a href="../what-our-customers-say.php">Read other Customer Reviews</a></p>
       <?php do { ?>
          <div class="clearfix"></div>
          <p class="feedback-quote"><?php echo $row_rsFeatured['FeedbackText']; ?></p>
          <p class="feedback-name"><strong><?php echo $row_rsFeatured['FeedbackSalutation']; ?> <?php echo $row_rsFeatured['FeedbackFirstName']; ?> <?php echo $row_rsFeatured['FeedbackLastName']; ?> | <?php echo $row_rsFeatured['FeedbackLocation']; ?></strong></p>
          <p class="feedback-date"><?php echo date('jS M Y',strtotime($row_rsFeatured['FeedbackDate'])); ?></p>
         <?php } while ($row_rsFeatured mysql_fetch_assoc($row_rsFeatured)); ?>
        <?php
  
// ("".$row_rsFeatured['FeaturedType']  ."" == "2")
?>




and obviously to determine the page i am using

php:
$basefile = basename ( $_SERVER["REQUEST_URI"] );


WHERE FeaturedLocale = '$basefile'



the basefile is stored in the featured table under FeaturedLocale

the ProductID OR FeedbackID is stored in the featured.FeaturedTypeID table.
AND FeaturedType 1 means a product and FeaturedType 2 means feedback
(currently im not using FeaturedPosition as of yet (but in the future this will allow the client to reposition content)

Chris

Sign in to reply to this post

Jason ByrnesWebAssist

Can you do an SQL Dump please so I can see the data and play with the SQL.

Sign in to reply to this post

Christopher WestCommunity Expert

Here we go I attached the files as private as well as the main .php file.

Thanks Jason

Sign in to reply to this post

Jason ByrnesWebAssist

i think i see the problem.

add the following code to the body of the page to output the SQL:

<?php echo($query_rsFeatured); ?>

post back that SQL please.

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...