INSERTING RANDOM ROTATING IMAGES ON A WEB PAGE - Aug 3rd, 2010
|
Inserting images or banners on a web page and having them change each time the page is reloaded is really easy.
For this example we’ll display one image at random from a simple multi record section editor. The fields in the editor are Title, an image upload field limited to one upload per record and a link URL that’s entered into the first extra information field (info1) in the upload advanced options.
The code that displays the images and links on the web page is identical to any other image display code:
<?php foreach ($your_tableRecords as $record): ?> <?php foreach ($record['image'] as $upload): ?> <a href="<?php echo $upload['info1'] ?>"> <img src="<?php echo $upload['thumbUrlPath2'] ?>" width="<?php echo $upload['thumbWidth2'] ?>" height="<?php echo $upload['thumbHeight2'] ?>" style="border:hidden" alt="" /></a> <?php endforeach ?> <?php endforeach ?>
The secret is using various options to determine how the image records in your_table will be displayed. For our situation we want to randomize the order of the images and then display only one image. We’ll use the option 'orderBy' => 'RAND()', to randomize the order and 'limit' => '1', to limit the number of images that can be displayed at a time. Here’s the code that would appear at the top of your PHP web page:
<?php header('Content-type: text/html; charset=utf-8'); ?> <?php require_once "/your_path_to/cmsAdmin/lib/viewer_functions.php";
list($your_tableRecords, $your_tableMetaData) = getRecords(array( 'Table name' => 'your_table', 'orderBy' => 'RAND()', 'limit' => '1', ));
If you wanted to have more than one group of images or banners display on your page, just create a second multi record editor (your_second_table) and insert the following at the top of your PHP page:
<?php header('Content-type: text/html; charset=utf-8'); ?> <?php require_once "/your_path_to/cmsAdmin/lib/viewer_functions.php";
list($your_tableRecords, $your_tableMetaData) = getRecords(array( 'Table name' => 'your_table', 'orderBy' => 'RAND()', 'limit' => '1', ));
list($your_second_tableRecords, $your_second_tableMetaData) = getRecords(array( 'Table name' => 'your_second_table', 'orderBy' => 'RAND()', 'limit' => '1', ));
and display the images from that table like this:
<?php foreach ($your_second_tableRecords as $record): ?> <?php foreach ($record['image'] as $upload): ?> <a href="<?php echo $upload['info1'] ?>"> <img src="<?php echo $upload['thumbUrlPath2'] ?>" width="<?php echo $upload['thumbWidth2'] ?>" height="<?php echo $upload['thumbHeight2'] ?>" style="border:hidden" alt="" /></a> <?php endforeach ?> <?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.