2012-03-02 155 views
-1

我很努力在每个项目之后插入逗号,如果有多个项目。插入逗号分隔输出

我有他们应该的所有值,只有缺少逗号。 ($ controller ['cv],array('controller'=>'postTags','action'=>'view',$ post_tags [$ ck]),array() 'title'=>'vis artikler under'。$ tags [$ cv],'escape'=> false)); echo $ result = substr($ result,0,-2);

此输出正确的链接没有逗号:test1test2test3 if multiple => should be; test1,test2,test3

此外,如果只有1项=>输出应该是test1(无逗号)。

所以,代码原样,输出正确的链接,但没有逗号!我不确定如何继续,有什么建议?

尝试过内爆(使用蛋糕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 ''; 
     } 

给我的错误..; //

PR($ post_tags);

Array 
(
    [0] => 3 
    [1] => 1 
    [2] => 2 
) 

pr($ tags);

Array 
(
    [1] => Tag1 
    [2] => Tag2 
    [3] => Tag3 
    [4] => Tag4 
) 

回答

2

UPDATE

// get only the tags assigned to the post 
$postTagKeys = array_flip($post_tags); 
$tags = array_intersect_key($tags, $postTagKeys); 

// ok lets make the links: 
$tagLinks = array(); 

foreach($tags as $tagId => $tagName) { 
    $tagLinks[] = $html->link(
     $tagName, 
     array('controller'=>'postTags','action' => 'view', $tagId), 
     array('title'=>'Vis artikler under '.$tagName,'escape' => false) 

    ); 
} 

//$tagLinks is now an array of html <a> tags linking to individual tags 
// so to ouput the list we do 

echo implode(', ', $tagLinks); 

如果你有类作为一个数组只需使用破灭:

$cats = array('test1','test2','test3'); 
$cats2 = array('test1'); 

echo implode(', ',$cats); 
echo implode(', ',$cats2); 

因此,使用您的示例代码:

  foreach($post_tags as $ck => $cv) { 
      if(isset($tags[$cv])){ 
       $ci = $ci+1; 
       $taglist = implode(', ', $tags[$cv]); 
       $result = $html->link($taglist, array(
        'controller'=>'postTags', 
        'action' => 'view', 
        $post_tags[$ck] // are you sure you want to pass the array here and not just the array key? 
       ), array(
        'title'=>'Vis artikler under '.$taglist, 
        'escape' => false) 
       ); 

       echo $result; 
      } 
     } 
+0

试过了,不行! ; /见主题。 – Tom 2012-03-02 15:56:11

+0

那是因为结果是一个字符串,它需要是一个数组。 – prodigitalson 2012-03-02 16:02:11

+0

yepp ..我想......任何建议?查看整个代码的主要主题。 – Tom 2012-03-02 16:04:57

1

您应该使用implode()这不正是你需要的:

加入数组元素用字符串

使用likle这样:

$commaSeparated = implode(',', $array); 
+0

试过了,没有工作;/ – Tom 2012-03-02 15:56:51

+0

@Tom你是什么意思“不工作”?如果你做'print_r($ result)'你会得到什么? – 2012-03-02 16:00:07

+0

这不是一个数组,不知道如何继续,请参阅完整代码的主要主题。 – Tom 2012-03-02 16:05:24

0

这就是你'寻找:

$result = $html->link($tags[$cv] . ((count($tags) > 1 && $ci > 0 && count($tags) != $ci) ? ', ' : ''), array(
           'controller' => 'postTags', 
           'action' => 'view', 
           $post_tags[$ck] 
         ), array(
           'title' => 'Vis artikler under ' . $tags[$cv], 
           'escape' => false 
         ) 
        ); 

顺便说一句,只是使用$ci++;而不是$ci = $ci+1;