Pulling Image Data from another Table

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

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: [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