0

我正在尝试实现类似 facebook 之类的小部件,它说:

You, Name1, Name2 and 20 other people like this

我获取了所有数据以显示此 HTML,但我似乎无法找到正确的算法来形成 HTML 字符串。

我的主要问题是我不知道何时放置and字符串或,(逗号)字符串。如果我只需要输入名称,它会起作用,但问题是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

注意这个and词后面没有任何东西,因为You应该在它后面,但我把它放在了开头。有什么帮助吗?我只需要逻辑,而不是实际代码。

4

2 回答 2

3

你可以尝试这样的事情:

$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';
于 2012-05-23T15:25:30.947 回答
1

Eduard,您只需将其放在$key++;循环的顶部并取出循环中的所有位置即可解决此问题$key + 1

我认为正在发生的事情$key + 1是假设有一个当前用户。

如果当前用户不在第一个 $limit 个条目中,此行也不会显示当前用户

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

您可以通过将其放在查找当前用户的代码之后来解决此问题。

在 Java 中(我知道,但它是我运行的)它看起来像:

    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);
于 2012-05-23T15:22:02.807 回答