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
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
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
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
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
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
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/