Answer the question
In order to leave comments, you need to log in
How to trim comma at the end of terms?
Hello everyone, a typical task, I can’t figure out how to do it simply.
There is an array of tags that need to be displayed, each separately (with its own id and name). Well, separated by commas, of course. I do so
<?php $tags_c = count($new->tags); if ($tags_c == 0): ?>
<span >Разное</span>
<?php else: ?>
<?php foreach ($new->tags as $tag):?>
<span data-id = "<?= $tag->id ?>"><?= $tag->name ?>,</span>
<?php endforeach; ?>
<?php endif; ?>
var last = $('.tags_news').find('span:last');
last.substring(0,last.length - 1);
Answer the question
In order to leave comments, you need to log in
1. Quite
2. It’s easier to store json configs in a string format in the database
3. You can store each widget/widget parameter in a table as a separate attribute (column), but in my opinion this option is trashier
Isn't it easier to do it right away in php?
The expression will supply a comma everywhere except the last, but you need to initialize the index ($i) outside of the foreach
<?php $i = 0; ?>
<?php foreach ($new->tags as $tag):?>
<span data-id = "<?= $tag->id ?>"><?= $tag->name ?><?=count($new->tags) != ++$i ? "," : ""?></span>
<?php endforeach; ?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question