views:

69

answers:

5

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

+4  A: 

You can buffer your output (the tags). Let's say you put them in an array

array_push($myTags, '<span>'.$tag.'</span>');

When you want to show them you just implode(",", $myTags).

Or you can buffer them in a string and then substr() on that string until you get rid of the last comma. Personally, I'd suggest the array.

Bogdan Constantinescu
I never knew array_push existed. Nice, thank you.
Jack Webb-Heller
A: 

If you want to do it manually, just prepend the comma before any tag but the first one. Use a control variable to detect the first tag.

$iteration = 0;
$first=true;
foreach ($tagarray as $tag) { ?>
<div class="tagbutton listv">
<span><?php 
if($first){$first=false;}
else{echo ",";}
 echo $tag; 
?></span>
</div>  
<?php 
$iteration++;
    if ($iteration == 4) {
        break;
    }
} ?>

If you want to use library functions, use implode as suggested by Cetra and Bogdan.

pakore
I've chosen this as the Accepted answer, because I'm working with a designer on this part of the site with no experience in PHP, and I think this method will be most readable/least confusing for him. Thank you for your help! I have also learnt from all the other suggested answers too :)
Jack Webb-Heller
+2  A: 

Using implode is better for outputting stuff like this to a string, and array_slice can be used to return the first four elements. Assuming your array is called tagarray:

echo "<span>" . implode("</span>,<span>", array_slice($tagarray,0,4)) . "</span>";

Obviously changing the "glue" to what you want. This negates the need for a foreach loop and may be faster.

Cetra
+1  A: 

I would separate the concerns of slicing 4 elements, transforming each element, and joining the transformed results together. This would render your code more readable.

// take 4 elements
$firstelements=array_slice( $tags, 0, 4 );

// how to transform an element
function tag_span($tag) {
    return "<span>$tag</span>";
}

// transform all needed elements
$spans=array_map( tag_span, $firstelements );

// join them together
$result=implode( ", ", $spans );

// output the result
echo $result;
xtofl
(credits: grouped the answers of Bogdan Constantinescu and Pekka together before adding my own sauce)
xtofl
Wow! The code is definitely more readable, I wouldn't have thought of doing it like this. Insightful!
Jack Webb-Heller
A: 

Solution based on your code would be

$iteration = 0;
$processarr = array_slice($tagarray,0,4);   //get tags you need,4 or less
foreach ($processarr as $tag) { ?> 
<div class="tagbutton listv"> 
<span><?php echo $tag; ?></span>, 
</div>   
<?php  
$iteration++;
if($iteration < count($processarr)) echo ",";
}?> 
SpawnCxy