My client wanted to use placeholder variables to insert images into text boxes from a master image library of 12 images.
Expanding on the recipe: http://www.thecmsbcookbook.com/recipedetail.php?407
and an old post on the Interactive Tools Forum: http://www.interactivetools.com/forum/forum-posts.php?71740 this is what I came up with.
1) Create a single record section called “Image Library” and create 12 image upload fields, limited to 1 image per field, set the size of thumbnail2 to suit your needs.
2) At the top of each viewer where you want the ability to insert images add the following code: Note, I placed the code in a file called _image_library.php and used the line, <?php include ("_image_library.php"); ?> in the head of the pages so I didn’t have to re-insert the code into each page.
<?php // Code to add images to text boxes
list($image_libraryRecords, $image_libraryMetaData) = getRecords(array( 'tableName' => 'image_library', 'where' => '', // load first record 'loadUploads' => true, 'allowSearch' => false, 'limit' => '1', ));
$image_libraryRecord = @$image_libraryRecords[0]; // get first record ?> <?php if ($image_libraryRecord['image_1']):?> <?php foreach ($image_libraryRecord['image_1'] as $index => $upload): ?> <?php $image1 = $upload['thumbUrlPath2'] ?> <?php endforeach ?> <?php endif ?> <?php if ($image_libraryRecord['image_2']):?> <?php foreach ($image_libraryRecord['image_2'] as $index => $upload): ?> <?php $image2 = $upload['thumbUrlPath2'] ?> <?php endforeach ?> <?php endif ?> <?php if ($image_libraryRecord['image_3']):?> <?php foreach ($image_libraryRecord['image_3'] as $index => $upload): ?> <?php $image3 = $upload['thumbUrlPath2'] ?> <?php endforeach ?> <?php endif ?> <?php if ($image_libraryRecord['image_4']):?> <?php foreach ($image_libraryRecord['image_4'] as $index => $upload): ?> <?php $image4 = $upload['thumbUrlPath2'] ?> <?php endforeach ?> <?php endif ?> <?php if ($image_libraryRecord['image_5']):?> <?php foreach ($image_libraryRecord['image_5'] as $index => $upload): ?> <?php $image5 = $upload['thumbUrlPath2'] ?> <?php endforeach ?> <?php endif ?> <?php if ($image_libraryRecord['image_6']):?> <?php foreach ($image_libraryRecord['image_6'] as $index => $upload): ?> <?php $image6 = $upload['thumbUrlPath2'] ?> <?php endforeach ?> <?php endif ?> <?php if ($image_libraryRecord['image_7']):?> <?php foreach ($image_libraryRecord['image_7'] as $index => $upload): ?> <?php $image7 = $upload['thumbUrlPath2'] ?> <?php endforeach ?> <?php endif ?> <?php if ($image_libraryRecord['image_8']):?> <?php foreach ($image_libraryRecord['image_8'] as $index => $upload): ?> <?php $image8 = $upload['thumbUrlPath2'] ?> <?php endforeach ?> <?php endif ?> <?php if ($image_libraryRecord['image_9']):?> <?php foreach ($image_libraryRecord['image_9'] as $index => $upload): ?> <?php $image9 = $upload['thumbUrlPath2'] ?> <?php endforeach ?> <?php endif ?> <?php if ($image_libraryRecord['image_10']):?> <?php foreach ($image_libraryRecord['image_10'] as $index => $upload): ?> <?php $image10 = $upload['thumbUrlPath2'] ?> <?php endforeach ?> <?php endif ?> <?php if ($image_libraryRecord['image_11']):?> <?php foreach ($image_libraryRecord['image_11'] as $index => $upload): ?> <?php $image11 = $upload['thumbUrlPath2'] ?> <?php endforeach ?> <?php endif ?> <?php if ($image_libraryRecord['image_12']):?> <?php foreach ($image_libraryRecord['image_12'] as $index => $upload): ?> <?php $image12 = $upload['thumbUrlPath2'] ?> <?php endforeach ?> <?php endif ?>
<?php $placeHolders = array("*img1*", "*img2*", "*img3*", "*img4*", "*img5*", "*img6*", "*img7*", "*img8*", "*img9*", "*img10*", "*img11*", "*img12*"); $replaceWith = array(@$image1, @$image2, @$image3, @$image4, @$image5, @$image6, @$image7, @$image8, @$image9, @$image10, @$image11, @$image12 ); ?>
This code creates an array called $placeHolders, containing the placeholders *img1* through *img12* and an array called $replaceWith, containing the variables $image1 through $image12
You can use any name for the placeholders and the variables. Just make sure that the names won’t appear in the text as well.
3) in the body of your page, (in this example an “about us” page, which gets it’s content from the text box field “about_content” in a single record editor called “about”) where you would normally use:
<?php echo $aboutRecord['about_content'] ?>
Insert the code:
<?php echo str_replace($placeHolders, $replaceWith, $aboutRecord['about_content']);?>
str_replace will look into the $aboutRecord['about_content'] field, and replace all instances of *img1* with $image1, all instances of *img2* with $image2, all instances of *img3* with $image3, etc.
This code will also work for a multi record section detail page. For a multi record list page, just change $aboutRecord['about_content'] to $record['about_content'].
4) Finally, in the “about_content” text box, insert the appropriate placeholder in a <div> exactly where you want the image to appear, and use CSS to style the <div> to suit your needs.
<div align="center" class=”your_class”><img src="*img4*"></div>
|