-1

我想要做的是获取保存在数据库中的某个链接的引用列表的数组输出。到目前为止,我得到了它,但无法获得相同推荐人的数量,而不是一次又一次地显示它们。
这是我写的函数:

<?php 

function get_links_ref($astats) {   
  $sExistedLink= '';    
  $sMyLinkSQL = "SELECT * FROM `log` WHERE `shorturl`='{$astats}' ORDER BY `click_id` DESC";    
  $aLinks = $GLOBALS['oSysDB']->getAll($sMyLinkSQL);    
  foreach ($aLinks as $astats => $aLinkInfo) {  
    $reff = $aLinkInfo['referrer']; 
    $areff = parse_url($reff);  
    if($areff['host'] == 't.co' || $areff['host'] == 'www.twitter.com' || $areff['host'] == 'api.twitter.com'){ 
      $areff['host'] = "Twitter";   
    }   
    if($areff['host'] == 'www.facebook.com' || $areff['host'] == 'm.facebook.com'){ 
      $areff['host'] = "Facebook";  
    }   
    if($areff['host'] == 'bit.ly' || $areff['host'] == 'lilink.it'){    
      $areff['host'] = "Direct";    
    }   
    $sExistedLink .= <<<EOF
      <p>{$areff['host']}</p>       
EOF;

  } 
  return $sExistedLink; 
}   

作为回报,我得到一个像这样的列表作为示例: Facebook
Twitter
Facebook
Facebook
Twitter

是否可以编辑此代码,因为我可以得到类似的东西,括号中相同推荐人的总数 Facebook (3)
Twitter (2)

4

2 回答 2

3

在遍历数组时保留一个计数器:

$counts = array();
foreach ($aLinks as $astats => $aLinkInfo) {    
    $reff = $aLinkInfo['referrer'];  
    $areff = parse_url($reff);  

    if($areff['host'] == 't.co' || $areff['host'] == 'www.twitter.com' || $areff['host'] == 'api.twitter.com'){ 
        $counts["Twitter"] = isset( $counts["Twitter"]) ? ($counts["Twitter"] + 1) : 1;
    }   
    if($areff['host'] == 'www.facebook.com' || $areff['host'] == 'm.facebook.com'){ 
        $counts["Facebook"] = isset( $counts["Facebook"]) ? ($counts["Facebook"] + 1) : 1;   
    }   
    if($areff['host'] == 'bit.ly' || $areff['host'] == 'lilink.it'){    
        $counts["Direct"] = isset( $counts["Direct"]) ? ($counts["Direct"] + 1) : 1;
    }
}

现在,您可以形成字符串以将其打印出来,如下所示:

foreach( $counts as $site => $count)
{
     $sExistedLink .= "<p>$site ($count)</p>";
}
于 2012-08-03T02:57:37.450 回答
2

使用您的确切代码并稍作修改

function get_links_ref($astats) {   
    $sExistedLink= '';  
    $sMyLinkSQL = "SELECT * FROM `log` WHERE `shorturl`='{$astats}' ORDER BY `click_id` DESC";  
    $aLinks = $GLOBALS['oSysDB']->getAll($sMyLinkSQL);  
    foreach ($aLinks as $astats => $aLinkInfo) {    
        $reff = $aLinkInfo['referrer']; 
        $areff = parse_url($reff);  
        if($areff['host'] == 't.co' || $areff['host'] == 'www.twitter.com' || $areff['host'] == 'api.twitter.com'){ 
            $areff['host'] = "Twitter"; 
        }   
        if($areff['host'] == 'www.facebook.com' || $areff['host'] == 'm.facebook.com'){ 
            $areff['host'] = "Facebook";    
        }   
        if($areff['host'] == 'bit.ly' || $areff['host'] == 'lilink.it'){    
            $areff['host'] = "Direct";  
        }   

        if(!isset($array_count[$areff['host']])) $array_count[$areff['host']] = 0;
        $array_count[$areff['host']]++;
    }

    foreach ($array_count as $key => $value) {
        $sExistedLink .= <<<EOF
<p>$key ($value)</p>     
EOF;   
    }
   return $sExistedLink;   
}
于 2012-08-03T03:01:09.037 回答