Pulling Image Data from another Table

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

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

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

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