REMOVE THE TRAILING COMMA AFTER LAST IMAGE IN A SERIES - Feb 5th, 2011


When re-purposing code for various image display applications to work with CMS Builder, you’ll need to remove the
trailing comma from the last entry in your image list so the application will work in IE.

Here’s one way to do it, using the rtrim function.

Let’s say that your code requires the following syntax for your list:

{ src: 'cmsAdmin/uploads/thumb/1_Detail_10.jpg' },
{ src: 'cmsAdmin/uploads/thumb/2_Detail_4.jpg' },
{ src: 'cmsAdmin/uploads/thumb/3_Detail_3.jpg' },
{ src: 'cmsAdmin/uploads/thumb/4_Detail_5.jpg' }


Just using the following would put a comma at the end of every entry, including the last one.


<?php foreach ($home_page_slide_showRecord['images'] as $upload): ?>
{ src: '<?php echo $upload['thumbUrlPath'] ?> ' },<?php endforeach ?>


Instead, try something like this:


<?php $output = ''; ?>

<?php foreach ($home_page_slide_showRecord['images'] as $upload) {
$output .= '{ src: ' . '"' .$upload['thumbUrlPath']. '"' . '}'. ',' ;
}
?>

<?php $output = rtrim($output,','); // remove trailing comma ?>

<?php print $output; ?>


It looks complicated, but it’s not. Here’s how it works, so you can modify the code to suit your particular needs.

The <?php $output = ''; ?> code clears the variable $output.

Then the $output variable $output .= '{ src: ' . '"' .$upload['thumbUrlPath']. '"' . '}'. ',' ; work like this.

The “.” (period) and the ‘ (single quote) before any character or group of characters, followed by another ‘
(single quote) says, “append these characters to the string you’re creating”.

So in this example, the $output variable would take the {scr: and the space following it, append a ” (double quote)
to it, then append an image url path to that, then append another “ (double quote) to that, then append a } (right
brace) and finally append a ‘ (comma) to complete the string. This would loop through the images and add the above to
the string for each one.

The next line:


<?php $output = rtrim($output,','); ?>


Is where the magic happens. It says, look at the string created for the $output variable and if the very last character
is a comma, remove it.

Then

<?php print $output; ?>


prints the final code to your page.









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.


Terms of Service