Separate List Output with Comma
4 posts by 2 authors in: Forums > CMS Builder
Last Post: November 2, 2018 (RSS)
By mark99 - November 2, 2018
One of my fields is a selection list and when I output it using this:
<?php echo $record['test'] ?>
I get this:
TEST1 TEST2 TEST3
But what I want is this:
TEST1, TEST2, TEST3
What's the best way to add the comma, albeit not on the final output item (e.g. TEST3 above as I don't want a comma on the very end of the line)?
By daniel - November 2, 2018
Hi mark99,
If you are using getRecords() to retrieve your record, you should be able to use the following:
<?php echo implode( ', ', $record['test:labels'] ); ?>
If you used a different method to access the record or "test:labels" is otherwise unavailable, this alternate method should also work:
<?php echo implode( ', ', listValues_unpack( $record['test'] )); ?>
Let me know if I can help with anything else!
Thanks,
Technical Lead
interactivetools.com
By mark99 - November 2, 2018
I figure out I can also do it with this, but which approach is better (yours or mine)?
<?php echo join(', ', getListLabels('isp_list', 'sfbb_broadband_type_premium', $isp_listRecord['sfbb_broadband_type_premium'])); ?>
By daniel - November 2, 2018
Hi mark99,
They're both roughly equivalent, though I'd generally recommend my first example, if only because it's simplest. (My second example will only work if the list's values are the same as its labels). However, there are a few edge cases where it might not work, in which case yours is also a good option to try.
(Also implode() and join() are different names for the same function, so there's no difference there at all)
Thanks,
Technical Lead
interactivetools.com