English 中文(简体)
像FBs"X,Y和Z 其他人像这样"这样的特写
原标题:Feature like FB s "X, Y and Z other people like this"

我试图执行一些东西,像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 = "&hearts; 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 本来应该一直在它之后, 但我把它放在开头。 有帮助吗? 我只需要逻辑, 而不是实际代码 。

最佳回答

你可以尝试这样的东西:

$likedBy = array( admin ,  eduard ,  jeremy ,  someoneelse );

// check if I like it and if so move me to the front
if (in_array($currentUsername, $likedBy)) {
  $me = array_search($currentUsername, $likedBy);
  unset($likedBy[$me]);
  array_unshift($likedBy,  You );
}

// remove anything after the limit
$extra = array_splice($likedBy, 3);

// the comma list
$html = implode( ,  , $likedBy);

// any extras? if so, add them here, if not rewrite the list so
// it s "You, Eduard and admin"
if (!empty($extra)) {
  $html .=   and  .count($extra);
} else {
  $lastguy = array_splice($likedBy, 1);
  $html = implode( ,  , $likedBy).  and  .$lastguy;
}

$html .=   like this ;
问题回答

Eduard, You can fix this simply by putting $key++; at the top of the loop and taking out all the places where you have $key + 1 in the loop.

我认为现在的情况是,$key+1 假设有一个当前用户。

如果当前用户不是在条目的第一个 $限制数中,此行也不会显示当前用户

if($key + 1 > $limit)
    break;

您可以在寻找当前用户的代码后放置此代码来修正此选项 。

在爪哇(我知道,

    List<String> users = Arrays.asList("Brian","Tom","Jack","John");
    int key = 0;
    String html = "";
    String currentUser = "Brian";
    int limit = 3;
    boolean foundCurrentUser = false;
    for (String user : users) {
        key ++;
        if (currentUser == user) {
            foundCurrentUser = true;
            continue;
        }
        if (key > limit) {
            continue;
        }
        html += user;

        if (key < users.size() - 1) {
            html += ",";
        } else if (key == users.size() - 1 && key != limit) {
            html += " and ";
        }
    }
    if (foundCurrentUser) {
        String userHTML = "You";
        if (key > 2) {
            userHTML += ", ";
        } else if (key == 1) {
            userHTML += " and ";
        }
        html = userHTML + html;
    }

    html = "Likeed by " + html;
    if (users.size() > limit ) {
        html += " and 3 other people";
    }
    System.out.println(html);




相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签