I am struggling to insert comma separated after each item, if more than one item.
I have all values as they should, only missing the comma.
$result = $html->link($tags[$cv], array(‘controller’=>’postTags’,’action’ => ‘view’, $post_tags[$ck]), array(‘title’=>’Vis artikler under ‘.$tags[$cv],’escape’ => false));
echo $result = substr($result,0,-2);
This output correct links without comma: test1test2test3 if multiple => should be; test1, test2, test3
Also if only 1 item => output should be test1 (no comma).
So, code as is, does output correct links, but no comma! I am not sure how to proceed, any suggestions?
Tried implode (full-code using cake 1.3);
$ci = 0;
$post_tags = explode(",", $content['Post']['tag_id']);
if(!empty($post_tags)){
foreach($post_tags as $ck => $cv) {
if(isset($tags[$cv])){
$ci = $ci+1;
$result = $html->link($tags[$cv], array('controller'=>'postTags','action' => 'view', $post_tags[$ck]), array('title'=>'Vis artikler under '.$tags[$cv],'escape' => false));
//pr($result);
$commaSeparated = implode(',',$result);
echo $commaSeparated;
}
}
} else {
echo '';
}
Give me error.. ;//
pr($post_tags);
Array
(
[0] => 3
[1] => 1
[2] => 2
)
pr($tags);
Array
(
[1] => Tag1
[2] => Tag2
[3] => Tag3
[4] => Tag4
)
UPDATE
If you have the categories as an array just use implode:
So using your example code: