Pulling Image Data from another Table

5 posts by 2 authors in: Forums > CMS Builder
Last Post: January 7, 2010   (RSS)

By theclicklab - December 28, 2009

Hi there,

Here is what I am trying to achieve:

I wish to upload a series of banner images in one table and then select those from a dropdown menu in another table for page content (This way I can just select a banner image to display rather than uploading each time a page is added).

The banner image table has two fields:
-Title
-Image (upload)

The page content table has the following setup for the dropdown:
-Display As: pulldown
-Get options from database
-Section Tablename: header_images_small
-Use this field for option values: num
-Use this field for option labels: title

So far so good - it's all working. My question is:

What do I insert into my viewer code to display the image?

Many thanks again :)

Re: [aquaman] Pulling Image Data from another Table

By theclicklab - December 30, 2009

OK, looks like I managed to work this out after sleeping on it BUT I need to show a default image if none selected to stop it throwing a mysql error.

This is what's inserted into the body of my aboutRecord page to pull the image from another table:

<?php
list($header_images_smallRecords, $header_images_smallMetaData) = getRecords(array(
'tableName' => 'header_images_small',
'where' => "num ='{$aboutRecord['static_image_2']}'",
'limit' => '1',
));
$header_images_smallRecord = @$header_images_smallRecords[0]; // get first record
?>

<?php foreach ($header_images_smallRecord['image'] as $upload): ?>
<?php if ($upload['isImage']): ?>
Image stuff code here
<?php endif ?>
<?php endforeach ?>

Re: [aquaman] Pulling Image Data from another Table

By Chris - January 4, 2010

Hi aquaman,

Sorry we didn't get back to you sooner! Winter holidays and all.

You can default to a specific header_images_small record like this:

<?php
$num = $aboutRecord['static_image_2'];
if (!$num) { $num = 123; } // default num
list($header_images_smallRecords, $header_images_smallMetaData) = getRecords(array(
'tableName' => 'header_images_small',
'where' => "num ='{$num}'",
'limit' => '1',
));
$header_images_smallRecord = @$header_images_smallRecords[0]; // get first record
?>


You'll want to replace 123 above with the record number of a header_images_small record you want to use as a default.

I hope this helps! Please let me know if you have any questions.
All the best,
Chris

Re: [aquaman] Pulling Image Data from another Table

By Chris - January 7, 2010

Hi aquaman,

You're setting a $num variable, but then using $aboutRecord['static_image_2'] in your where clause. Try this? (changes in red)

$num = $aboutRecord['static_image_2'];
if (!$num) { $num = 47; } // default num
list($header_images_smallRecords, $header_images_smallMetaData) = getRecords(array(
'tableName' => 'header_images_small',
'where' => "num ='$num'",
'limit' => '1',
));
$header_images_smallRecord = @$header_images_smallRecords[0]; // get first record
if (!$header_images_smallRecord) { print "Could not find a header_images_small record for num = '$num'"; exit; }


I hope this helps. Please let me know if you have any questions.
All the best,
Chris