Hi folks,
So this is pretty basic, I know! Sorry, just one of those days.
I've got an array of tags collected from a database. There could be any number of tags in this array. However, I only want to output 4.
So I currently have this code:
$iteration = 0;
foreach ($tagarray as $tag) { ?>
<div class="tagbutton listv">
<span><?php echo $tag; ?></span>,
</div>
<?php
$iteration++;
if ($iteration == 4) {
break;
}
} ?>
You'll see after </span>
there's a ,
comma. Obviously this looks weird if the output looks like this:
tag1, tag2, tag3,
With a trailing comma. So I thought I could put this where the comma currently is:
<?php if ($iteration < count($tagarray) {echo ",";} ?>
That works, but, only when the count of $tagarray
is greater than 4 or something. And, like I say, $tagarray could be any value.
I also tried
<?php if ($iteration == 0 || $iteration == 1 || $iteration == 2 || $iteration == 3) {echo ",";}?>
Which, although a bit repetitive, should work, doesn't, because $tagarray could contain 2 tags, and therefore still have a trailing comma.
I realise this is probably a simple one, but hey, I really appreciate the help!
Thanks!
Jack