Peter Knight Posted November 19, 2014 Share Posted November 19, 2014 I have two sets of tags and I'm manually adding a comma after the echo which works but adds a comma to the very last tag too. {echo "<i class=\"fa fa-globe\"></i>"; foreach ($update->client_location as $geo) {echo "{$geo->title},";} {echo "<i class=\"fa fa-tags\"></i>"; foreach ($update->blog_tags as $tags) {echo "{$tags->title},";} You can see in the above screen grab that this works but adds a comma to the last word too. It's not a huge deal but I'd prefer the last word not to have a comma. I think it's called "implode" in PHP when you take an array and automagically separate each value with a comma? Would I end up with exactly the same issue by using an implode function? Link to comment Share on other sites More sharing options...
kongondo Posted November 19, 2014 Share Posted November 19, 2014 You need to 'read' MarkupBlog code . Search for rtrim to see an example.. rtrim() http://php.net/manual/en/function.rtrim.php 1 Link to comment Share on other sites More sharing options...
Joss Posted November 19, 2014 Share Posted November 19, 2014 If you want a more idiotic way of doing it, you could add the comma with CSS and just not do it for the last child...... (PS: Idiotic because it separates out the content into two places, but worth mentioning.) You could also identify the last iteration and not add a comma for it. So, if NOT last, then add comma, would be the way.... PPS: Going by the questions, does this mean you got some work??? Link to comment Share on other sites More sharing options...
Peter Knight Posted November 19, 2014 Author Share Posted November 19, 2014 I'm actually redoing my own site Joss Link to comment Share on other sites More sharing options...
Martijn Geerts Posted November 19, 2014 Share Posted November 19, 2014 I prefer to do it this way: this, another, that & those $out = ''; $count = count($update->client_location); foreach ($update->client_location as $key => $geo) { $div = ($key + 1 === $count) ? ($count === 1 ? '' : ' & ') : ($key === 0 ? '' : ', '); $out .= $div . $geo->title; } echo $out; 5 Link to comment Share on other sites More sharing options...
Joss Posted November 19, 2014 Share Posted November 19, 2014 I'm actually redoing my own site Joss Ah, filling in ..... Martijn, the more I look at your solution, the cleverer it gets It is also one of those that I would put a big comment above because when I look at it again in a year's time I would be thinking, "Now, what the hell was I trying to do there?" 1 Link to comment Share on other sites More sharing options...
Soma Posted November 19, 2014 Share Posted November 19, 2014 You can also PW API since 2.4 or 2.5 echo $page->blog_tags->implode(", ", "title"); 4 Link to comment Share on other sites More sharing options...
Peter Knight Posted November 20, 2014 Author Share Posted November 20, 2014 I prefer to do it this way: this, another, that & those $out = ''; $count = count($update->client_location); foreach ($update->client_location as $key => $geo) { $div = ($key + 1 === $count) ? ($count === 1 ? '' : ' & ') : ($key === 0 ? '' : ', '); $out .= $div . $geo->title; } echo $out; Thanks Martijn That's pretty cool. I can kind of make out whats happening there. Love the ampersand at the end too. Link to comment Share on other sites More sharing options...
Peter Knight Posted September 1, 2015 Author Share Posted September 1, 2015 You can also PW API since 2.4 or 2.5 echo $page->blog_tags->implode(", ", "title"); I really like the simpler markup here and I'm using it to echo a series of blog categories separate by a , <?php $page->blog_category->implode(", ", "title"); ?> Im unsure though how to wrap the title in a link. I'm thinking I first need to create a variable called $cats and associate it with my blog_category field. I then need to place an anchor before the title. <?php $cats = $page->blog_category; echo $page->blog_category->implode(", ", "<a href=\"$cats->url\">title</a>"); ?> As this produces no output and no error, I'm clearly doing something wrong. Looking at Ryans example of the new API, I can modify an example to include the url <?php $cats = $page->blog_category; echo $cats->implode(function($cat) { return "<a href='$cat->url'>$cat->title</a>"; }); ?> I know I'm close! Any help much appreciated. Link to comment Share on other sites More sharing options...
LostKobrakai Posted September 1, 2015 Share Posted September 1, 2015 You've two options here: using string-tag-replacement or a function like you tried <?php $cats = $page->blog_category; echo $cats->implode(", ", "<a href='{url}'>{title}</a>"); // or (notice the first implode argument) echo $cats->implode(", ", function($cat) { return "<a href='$cat->url'>$cat->title</a>"; }); Detailed instructions about how these api functions work can be found here: https://processwire.com/talk/topic/5098-new-wirearray-api-additions-on-dev/ Link to comment Share on other sites More sharing options...
Peter Knight Posted September 1, 2015 Author Share Posted September 1, 2015 Thanks. I was almost there. I can see where I went wrong with the second example too. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now