To join or to explode that is the question

4 posts by 3 authors in: Forums > CMS Builder
Last Post: April 17, 2012   (RSS)

Re: [justritedesign] To join or to explode that is the question

By (Deleted User) - April 16, 2012

Hi Michael,

Using 'join' creates a string from a list using the character(s) selected to attach them (in this case a comma).

Using Explode does the reverse, with the arguments otherwise the same.

What I think you're aiming to do is this:
<?php foreach ($servicesRecords as $record): ?>
<?php $tags = explode(',',getListLabels('services', 'keywords', $record['keywords'])); ?>
<?php foreach ($tags as $tag ) : ?>
<a class="tag-body yellow" href="#"><span class="tag"><?php echo $tag; ?></span></a>
</php endforeach; ?>
<?php endforeach ?>


This will create a link for each tag from each record.

Let me know if this helps,

Tom

Re: [Tom P] To join or to explode that is the question

By Mohaukla - April 16, 2012

Thanks Tom,
Im hoping this was a typo problem:

<?php foreach ($servicesRecords as $record): ?>
<?php $tags = explode(',',getListLabels('services', 'keywords', $record['keywords'])); ?>
<?php foreach ($tags as $tag ) : ?>
<a class="tag-body yellow" href="#"><span class="tag"><?php echo $tag; ?></span></a>
</php endforeach; ?>
<?php endforeach ?>

I am thinking the /endforeach; is actually a ?endforeach .... Not sure at all about the ; ....

I tried to fix it and got verified code with and without the ; But returned an error message: PHP Notice: Array to string conversion

So I see its close to a fix but I need a little more help [laugh]

Thanks Michael
Michael Moyers



Owner of Just Rite Design Inc. A growing network of professionals in web design, graphic design, flash development, programming, and audio & video productions.



"Due to budget constraints, the Light at the end of the tunnel will be temporarily out!"

Re: [justritedesign] To join or to explode that is the question

By Jason - April 17, 2012

Hi Michael,

You're right about the typo, it should be a "?" instead of a "/". The semi-colon at the end of the "endforeach" statement isn't technically necessary, but it won't cause any errors either. The semi colon is just used to end a statement, but in this case the ?> would end the statement anyway. It's a more a matter of style than anything.

The reason you're receiving the error is that explode() expects a string for it's second argument, but getListLabels() returns an array. If the "keywords" field is a multi value list (either drop down or checkbox) you can actually access all the labels using the :labels pseudo field like this:

<?php foreach ($servicesRecords as $record): ?>

<?php foreach ($record['keywords:labels'] as $tag ) : ?>
<a class="tag-body yellow" href="#"><span class="tag"><?php echo $tag; ?></span></a>
<?php endforeach; ?>

<?php endforeach ?>


Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/