Merging Tables - A Practical Concept?

13 posts by 6 authors in: Forums > CMS Builder
Last Post: February 20, 2011   (RSS)

Hello, All -

This is just a concept. I'd like to know if it's worth pursuing.

In simple terms, is it possible to merge the data from different tables?

The tables would be naming using a strict naming convention and would each be composed of the same fields - with one or two exceptions.

For example, there could be 3 tables with the following fields:

Table1
Title
Content

Table2
Title
Content
Author

Table3
Content
Upload
Attachment

If I were able to merge the output of all three tables I would (in principle) be able to call the following data:

$record['title']
$record['content']
$record['author']
$record['upload']
$record['attachment']


Is it a practical concept? Would there be a clean way of achieving the goal?

:o|

Perch

Re: [Perchpole] Merging Tables - A Practical Concept?

By Jason - November 4, 2010

Hi Perch,

You can use a join in your query, but you need a common field across each table so you know which records your combining.

Let me know how you're deciding which records to combine and we'll see what we can work out.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Merging Tables - A Practical Concept?

By Toledoh - November 5, 2010

Can I jump on the wagon?

I've got a jQuery slider showing images and captions from the listings table.

http://d1008284.my.ozhosting.com/index.php

Rather than just property listings, I also want to show results from the images table, and possibly other tables.

All tables will have a multitude of fields, however, the "heroImage" will always hold an image of 333px square, and a "title" field.

I need to display;
<li>
<a href="_link">
<img src="heroImage" width="333" height="333" border="0" class="captify" rel="caption[num]">
<div id="caption1">[title]</div>
</a>
</li>


Is this similar to your requirements Perch?
Cheers,

Tim (toledoh.com.au)

Re: [Jason] Merging Tables - A Practical Concept?

Hey guys,

Any thoughts on this at all?
Cheers,

Tim (toledoh.com.au)

Re: [Dave] Merging Tables - A Practical Concept?

Hi Dave.

Sorry for the delay in getting back to you (especially after I rushed you...[blush] )

I've attached the viewer - the pertinent code starts at line 138;
<!--carousel -->
<ul id="mycarousel" class="jcarousel-skin-tango">
<?php foreach ($imagesRecords as $record): ?>
<!--Should be for each imagesRecords or listingsRecords where "homePage" is ticked orderBy RAND-->
<li><a href="">
<?php foreach ($record['hero_shot'] as $upload): ?>
<img src="<?php echo $upload['urlPath'] ?>" width="333" height="333" border="0" class="captify" rel="caption<?php echo $record['num'] ?>">
<div id="caption<?php echo $record['num'] ?>"><?php echo $record['title'] ?></div>
<?php break ?><!--Only 1 image per listing shown-->
<?php endforeach ?>
</a>
</li>
<?php endforeach ?>
</ul>
<!--carousel -->


I want to show
- records from the listings table and the images table,
- only when the homePage checkbox is ticked
- display them in a random order

I'm still up in the air about the "link" aspect... so I will sort that later, and there's no need to do the paging aspect.

Thanks!
Cheers,

Tim (toledoh.com.au)
Attachments:

index2_001.php 9K

Re: [Toledoh] Merging Tables - A Practical Concept?

By Jason - November 23, 2010

Hi Tim,

First, you can control the records being returned up at the top of your page. For example, if you only want listing records where "homePage" has been checked and you want a random order, you can use this:

list($listingsRecords, $listingsMetaData) = getRecords(array(
'tableName' => 'listings',
'orderBy' => "RAND()",
'where' => "homePage = 1",
));


After you've selected all your records, you can merge them into a single array that you can use to output your images. NOTE: This will only work if your upload fields have the same name and both have a field called "title".

EXAMPLE:
$carouselRecords = array_merge($imagesRecords,$listingsRecords);

Finally, you use this array to output your images:

<!--carousel -->
<ul id="mycarousel" class="jcarousel-skin-tango">
<?php foreach ($carouselRecords as $record): ?>
<li><a href="">
<?php foreach ($record['hero_shot'] as $upload): ?>
<img src="<?php echo $upload['urlPath'] ?>" width="333" height="333" border="0" class="captify" rel="caption<?php echo $record['num'] ?>">
<div id="caption<?php echo $record['num'] ?>"><?php echo $record['title'] ?></div>
<?php break ?><!--Only 1 image per listing shown-->
<?php endforeach ?>
</a>
</li>
<?php endforeach ?>
</ul>
<!--carousel -->


Give this a try and let me know if you run into any issues.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Merging Tables - A Practical Concept?

Thanks Jason - that's brilliant!

Next... is it possible to randomise the $carouselRecords?

At the moment, it's randomising the listings, then randomising the images, then joining them which means $carouselRecords has all the listings, then all the images rather than a random mix of listings and images.
Cheers,

Tim (toledoh.com.au)

Re: [Toledoh] Merging Tables - A Practical Concept?

By Jason - November 23, 2010

Hi Tim,

Sure, you can use the php shuffle() function to randomize your array:

$carouselRecords = array_merge($imagesRecords,$listingsRecords);
shuffle($carouselRecords);

---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Merging Tables - A Practical Concept?

Perfect - that's a heap!
Cheers,

Tim (toledoh.com.au)