0

我是php新手,所以请善待。

我正在构建一个脚本,该脚本从 facebook 页面获取 facebook 喜欢的数量。

然后对它们进行排序,我找到了一种使用 css 添加页面的个人资料图片的方法,但是我能够添加的唯一类是 url。我怎样才能给每个缩略图它自己的类,然后我可以将 css 应用到?

这是我的代码:

function array_sort($array, $on, $order=SORT_ASC)
{
$new_array = array();
$sortable_array = array();

if (count($array) > 0) {
    foreach ($array as $k => $v) {
        if (is_array($v)) {
            foreach ($v as $k2 => $v2) {
                if ($k2 == $on) {
                    $sortable_array[$k] = $v2;
                }
            }
        } else {
            $sortable_array[$k] = $v;
        }
    }

    switch ($order) {
        case SORT_ASC:
            asort($sortable_array);
        break;
        case SORT_DESC:
            arsort($sortable_array);
        break;
    }

    foreach ($sortable_array as $k => $v) {
        $new_array[$k] = $array[$k];
    }
  }

  return $new_array;
  }


  function getLikes($arr){
$urls = "";

// Add urls to check for likes
for($i = 0;$i < count($arr);$i++) {
    if($urls != "") $urls .= ",https://www.facebook.com/";
    $urls .= $arr[$i];
}

// Retreive info from Facebook 
$xml = simplexml_load_file("http://api.facebook.com/restserver.php?method=links.getStats&urls=https://www.facebook.com/" . $urls);

$likes = array();
// Loop through the result and populate an array with the likes
for ($i = 0;$i < count($arr);$i++) {
    $url = $xml->link_stat[$i]->url;

    $counts = (int)$xml->link_stat[$i]->like_count;         
    $likes[] = array('likes' => $counts,'url' => $url);number_format(1000, 0, '.', ',');
}   

return $likes;

 }

 $array =      array("kylieminogue","SiaMusic","iggyazalea");
 $likes = getLikes($array);




 $likes = array_sort($likes, 'likes', SORT_DESC); 



 foreach ($likes as $key => $val) {

 $final = number_format($val['likes'], 0, '.', ',');

 echo "<li class='facebook'><div class='fb-page'><div class='rank'>" . $key . "</div>" . "<div class='thumb "  . $val['url'] . "'><div class='link'>" . $val['url'] . "</div></div>" . "<div class='likes'>" . $final . "</div></div></li><br />";

 }
4

1 回答 1

1

如果您在 getLikes() 中执行此操作,则在第二个循环中:

$likes[] = array(
    'likes' => $counts,
    'url'   => $url,
    // create a hopefully unique class name
    'class' => strtolower($arr[$i]) . '-' . $i
);
// After this you call number_format without receiving its value, why?

然后在你改变的 HTML

"<div class='thumb "  . $val['url'] . "

为了

"<div class='thumb "  . $val['class'] . "

你是这个意思吗?

于 2013-10-19T03:42:34.333 回答