Trim last item?

9 posts by 3 authors in: Forums > CMS Builder
Last Post: November 19, 2015   (RSS)

By Djulia - November 18, 2015 - edited: November 19, 2015

Hi Tim,

Try this :

<?php
    $str = array();
    foreach ($home_pageRecord['hero_image'] as $index => $upload) {
        $str[] = '{img: '. htmlencode($upload['urlPath']) .', caption: '. htmlencode($upload['info1']). '}';
    } echo implode(",", $str), "\n\n";
?>

Thanks!

Djulia

By Toledoh - November 19, 2015 - edited: November 19, 2015

Thanks Djulia.

I think you missed a bracket "(", but it's also trimming both commas, not just the last one?

<?php 

foreach ($home_pageRecord['hero_image'] as $index => $upload) {
echo rtrim('{img: '. htmlencode($upload['urlPath']) .', caption: '. htmlencode($upload['info1']). '},', ',');
}
?>

is returning

{img: /~thelakes/cmsb/uploads/s1.jpg, caption: This is slide 1}{img: /~thelakes/cmsb/uploads/s2.jpg, caption: heres another slide}];

and needs to return

{img: /~thelakes/cmsb/uploads/s1.jpg, caption: This is slide 1},{img: /~thelakes/cmsb/uploads/s2.jpg, caption: heres another slide}];

Cheers,

Tim (toledoh.com.au)

By Djulia - November 19, 2015 - edited: November 19, 2015

Oops! I am not really awaked!

I have the feeling which you wish to obtain from the data encoded in json.

Can you test this?

<?php
    $str = array();
    foreach ($home_pageRecord['hero_image'] as $index => $upload) {
        $str[] = '{img: '. htmlencode($upload['urlPath']) .', caption: '. htmlencode($upload['info1']). '}';
    }

    //echo json_encode($str, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE), "\n\n";
    echo json_encode($str), "\n\n";
?>

Hey Djulia.

I appreciate your help!

I am now getting the error: Notice: Array to string conversion in /home/thelakes/public_html/index.php on line 161Array];

If it helps, the complete code that I require is as below, and I'm trying to produce the highlighted.

<script>

var items = [
{ img: "image1.jpg", caption: "This is the first caption."},
{ img: "image2.jpg", caption: "This is the second caption."}
];

var images = $.map(items, function(i) { return i.img; });

$.backstretch(images);
$(window).on("backstretch.show", function(e, instance) {
var newCaption = items[instance.index].caption;
$(".caption").text( newCaption );
});

</script>

Cheers,

Tim (toledoh.com.au)

By Djulia - November 19, 2015

With the version json?

Can you state that there is on line 161?

Thanks!

By Daryl - November 19, 2015

Hi Tim,

Another way to do it is to check if the current array is the last item. If not, then add a comma.

For example:

$lastUploadRecord      = end($home_pageRecord['hero_image']); // get the last record
$imageAndCaptionString = '';
foreach ($home_pageRecord['hero_image'] as $index => $upload){
  $imageAndCaptionString .= '{img: "'.htmlencode($upload['urlPath']).'", caption: "'.htmlencode($upload['info1']).'"}';
  if ($upload['num'] != $lastUploadRecord['num']){
    $imageAndCaptionString .= ','; // add comma if this is not the last upload record
  }
}
showme($imageAndCaptionString);

Let me know if that works for you.

Thanks,

Daryl Maximo
PHP Programmer - interactivetools.com

Hey Daryl - that worked thanks!  I changed showme($imageAndCaptionString); to echo($imageAndCaptionString);

Cheers,

Tim (toledoh.com.au)