To join or to explode that is the question

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

By Mohaukla - April 15, 2012

I have created a list of keywords that can be used throughout the site by adding an Advanced list to whatever page requires it and then adding to the pool of keywords as needed.

What I am having trouble with is when I want to separate these keywords to display them in a "Tag" Section. Each one is displayed in a CSS tag.
<?php foreach ($servicesRecords as $record): ?>
<a class="tag-body yellow" href="#"><span class="tag"><?php echo join(', ', getListLabels('services', 'keywords', $record['keywords'])); ?></span></a>
<?php endforeach ?>


What I am getting is one tag displayed but with all the keywords in comma separated format.
I believe that I need to use an "explode" instead of a "join" (Correct me if I'm wrong...) But I am unclear on how that looks and if there is more coding to it. The example above works great for the page code meta tag "keywords" as it even has the comma separation in the echo. But not for what I am attempting elsewhere in the page display.

Thanks in advance for your help
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 (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!"