SHOWING IMAGES FROM ANOTHER TABLE ON YOUR PAGES - Aug 3rd, 2010
|
Aquaman had this interesting solution that he shared with us on the forum.
He had a series of banner images and he wanted to be able to specify which ones to display on a series of detail pages without the need to upload the images each time a page was added.
Here’s how you can do the same:
First, create a section editor (called My Images” for this recipe) with two fields to act as your image “library”:
-Title -Image (upload)
The upload field is set for a maximum of 1 upload and the extra info field labels are removed. I’d suggest setting this as a required field. You can set the other values and thumbnail sizes to suite your needs.
In the section where you want to show the images, (Which I called “My Pages” for this recipe), you’d use the following setup for a field (which I called Banner Image):
-Field Type: list -Display As: pull-down -Get options from database -Section Table name: my_images -Use this field for option values: num -Use this field for option labels: title
Before the <head> of the detail page where you want’s to show the images you’d used the following code:
<?php header('Content-type: text/html; charset=utf-8'); ?> <?php require_once "/hsphere/local/home/gkornblu/thecmsbcookbook.com/cmsAdmin/lib/viewer_functions.php";
list($my_pagesRecords, $my_pagesMetaData) = getRecords(array( 'Table name' => 'my_pages', 'where' => whereRecordNumberInUrl(1), 'limit' => '1', )); $my_pagesRecord = @$my_pagesRecords[0]; // get first record
// show error message if no matching record is found if (!$my_pagesRecord) { header("HTTP/1.0 404 Not Found"); print "Record not found!"; exit; }
?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
Then in the body of the page where you want’s to show the images you’d used the following code:
<?php list($my_imagesRecords, $my_imagesMetaData) = getRecords(array( 'Table name' => 'my_images', 'where' => "num ='{$my_pagesRecord['banner_image']}'", 'limit' => '1', )); $my_imagesRecord = @$my_imagesRecords[0]; // get first record ?>
And finally where the image is displayed (must be below the code above on your page) <?php foreach ($my_imagesRecord['image'] as $upload): ?> <img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="" /> <?php endforeach ?>
If for some reason you wanted to display the images from the “banner library” on a list page, the code before the <head> tag would change to:
<?php header('Content-type: text/html; charset=utf-8'); ?> <?php require_once "/your_path_to/cmsAdmin/lib/viewer_functions.php";
list($my_pagesRecords, $my_pagesMetaData) = getRecords(array( 'Table name' => 'my_pages', ));
?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
And you’d need to include the code from the detail page in your foreach loop, and change the:
where' => "num ='{$my_pagesRecord['banner_image']}'",
to
'where' => "num ='{$record['banner_image']}'",
Your code would then look like this:
<?php foreach ($my_pagesRecords as $record): ?> <?php list($my_imagesRecords, $my_imagesMetaData) = getRecords(array( 'Table name' => 'my_images', 'where' => "num ='{$record['banner_image']}'", 'limit' => '1', )); $my_imagesRecord = @$my_imagesRecords[0]; // get first record ?> <?php foreach ($my_imagesRecord['image'] as $upload): ?> <img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="" /> <?php endforeach ?> <hr/> <?php endforeach ?>
|
The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact
Interactive Tools for information on the downloading of the software or the purchasing of licenses.