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 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: [chris] Pulling Image Data from another Table

By theclicklab - January 7, 2010

Hi Chris, I think I'm almost there, I'm getting this error now:

Warning: Invalid argument supplied for foreach() see code below for "Error showing here"

<?php elseif ($aboutRecord['header_type'] == 'static'): ?>
<?php
$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 ='{$aboutRecord['static_image_2']}'",
'limit' => '1',
));
$header_images_smallRecord = @$header_images_smallRecords[0]; // get first record
?>
<div class="feature">
<?php foreach ($header_images_smallRecord['image'] as $upload): ?> // Error showing here
<?php if ($upload['isImage']): ?>
<img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt="<?php echo $header_images_smallRecord['title'] ?>" />
<?php endif ?>
<?php endforeach ?>
</div>
<?php endif ?>

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