How to remove a comma off the end in the last listing?

3 posts by 2 authors in: Forums > CMS Builder
Last Post: August 14   (RSS)

By Tom - August 14 - edited: August 14

Thanks in advance for your help.

I intend to remove the last comma in the last string.

I did try rtrim but no success.

<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "ItemList",
"itemListElement": [
<?php $counter = 1; ?>
<?php foreach ($listingRecords as $listing): ?>
<?php foreach ($listing['uploads'] as $upload): ?>
{
"@type": "ListItem",
"position": "<?php echo $counter; ?>",
"item": {
"@type": "Movie",
"url": "<?php echo $listing['_link'] ?>",
"name": "<?php echo $listing['ref_no1'] ?>",
"image": "<?php echo $upload['urlPath'] ?>",
"dateCreated": "<?php echo $listing['date_added'] ?>",
"director": {
"@type": "Person",
"name": "abcdefg"
}
}
},
<?php $counter++ // increment counter ?>
<?php endforeach ?> <?php endforeach ?>
]
}
</script>

It's all about the output of the last listing.

There is a comma in the last part of listing.

"@type": "Person",
"name": "abcdefg" 
}
}
},  <-- remove this comma in the last listing-->
]
}
</script>

Is there any method to remove the comma in the last listing?

Thanks a lot and sorry for trouble.

By gord - August 14

I think the easiest way to do this is to simply populate an array and then use json_encode() to output it...

Gord

By Tom - August 14

Hello,

Thanks for your info and I've just use a stupid way to solve it.

Thanks again

"director": {
                "@type": "Person",
                "name": "abcdefg"
              }
            }
          }<?php if($listingDetails['totalRecords'] != $counter)
            { echo ",";} ?> 
		   <?php $counter++ ?>
               <?php endforeach ?>
      ]
    }
    </script>