Using placeholder variables to insert images into text boxes

4 posts by 2 authors in: Forums > CMS Builder
Last Post: May 17, 2017   (RSS)

By gkornbluth - April 15, 2017

Hi All,

I’m using placeholder variables to insert images from a single record “Image Library” into various text boxes on a site, and I’d really like to make the code I'm using both more elegant and dynamic.

Right now, as you’ll see from the code below, the code that assigns images to the variables is hard coded and pretty repetitive, and unless I create a whole other set of placeholders and variables, I can only use thumbUrlPath2. Adding additional images to the library is also a hard coded process.

Anyone have an idea how I can streamline the process and make the code more dynamic so that I can use other Thumbnails and my client can add more placeholders and variables as needed?

(I'm pretty sure that a multi record "Image Library" would be a place to start, but I don't know how to use that to dynamically create the placeholders and assign the variables required.)

Thanks,

Jerry Kornbluth

Here’s the current code that creates the placeholders and assigns the variables (it’s included into each viewer where I want to use images). There are actually 12 placeholders and image fields, but I’ve only shown 3 in this example:

<?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 $placeHolders = array("*img1*", "*img2*", "*img3*");
$replaceWith = array(@$image1, @$image2, @$image3); ?>

Here’s the code that’s inserted into the viewer(s):

<?php echo str_replace($placeHolders, $replaceWith, $my_sectionRecord['my_text_box_contents']);?>


And here’s the code that’s inserted into the text box to load the placeholder for $image_4.

<div class=”my_class”><img  src="*img4*"></div>





The first CMS Builder reference book is now available on-line!

Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By gkornbluth - May 15, 2017

Thanks Ross,

Interesting.

Any idea what we're talking about in terms of cost?

Jerry

The first CMS Builder reference book is now available on-line!

Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By ross - May 17, 2017

Hi Jerry

There are a few more things to go over.

Could you send me an email to consulting@interactivetools.com (with FTP and CMS login) and we'll continue the conversation there?

Thanks.

-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/