我试图执行一些东西,像facebookss 像部件,说的东西,比如:
You, Name1, Name2 and 20 other people like this
我收集了全部数据来显示这个 HTML, 但我似乎找不到合适的 algo 来组成 HTML 字符串 。
我的主要问题是,我不知道何时要放 和
字符串或 、
(comma) 字符串。 如果我只需要放名字, 它就会起作用, 但问题是, You
字符串总是必须先放。
我要在这里粘贴我的代码 以及我为一些特殊病例得到的输出(这是PHP)
$current_user = 0;
$html = "";
$count = count($result);
$key = 0;
foreach($result as $liked){
if($key + 1 > $limit)
break;
if($liked->uid == $user->uid){
$current_user = 1;
continue;
}
$html .= "<a href= ".$liked->href." >".$liked->name."</a>";
if($key < $count - 2)
$html .= ", ";
elseif($key == $count - 2 && $key + 1 != $limit)
$html .= " and ";
$key++;
}
if($current_user){
$userHtml = "You";
if($count > 2)
$userHtml .= ", ";
elseif($count > 1)
$userHtml .= " and ";
$html = $userHtml.$html;
}
$html = "♥ by ".$html;
if($count > $limit){
$difference = $count - $limit;
$html .= " and ".$difference." ".format_plural($difference,"other","others");
}
return $html;
在特例中,当当前用户是最后一个喜欢这个的用户时, 它会显示:
♥ by You, admin, edu2004eu and
注意 < code> 和 < / code > 单词之后没有任何东西, 因为 < code> You code > 本来应该一直在它之后, 但我把它放在开头。 有帮助吗? 我只需要逻辑, 而不是实际代码 。