2011-12-25 114 views
0

突然我在索引頁沒有變化發生在所有得到這個錯誤顯示,並正在爲幾個月,PHP的錯誤 - 在array_multisort

PS:我的直播網站:sudanesetweeps.com

array_multisort() [function.array-multisort]: Argument #1 is expected to be an array or a sort flag in

Warning: Invalid argument supplied for foreach() in

我用下面的代碼:

<?php 

require_once("dbconnect.php"); 
$query = " SELECT COUNT(*) cnt, hashtags 
FROM tweets 
WHERE tweeted_at > DATE_SUB(NOW() , INTERVAL 1 DAY) 
AND hashtags != '' 
GROUP BY hashtags 
ORDER BY cnt DESC "; 
$res = mysql_query($query); 
while($row = mysql_fetch_assoc($res)) { 
$count = $row['cnt']; 
$hashtags = explode(" ", $row['hashtags']); 
foreach($hashtags as $hashtag) { 
//$index = array_search($hashtag, array_keys($topics)); 
    if(strtolower($hashtag) != 'gmaes' && strtolower($hashtag) != 'lord'  ) 
     $topics[strtolower($hashtag)] += $count; 
    } 
} 

array_multisort($topics, SORT_DESC); 

echo "<ul id='mytags'>"; 
$index = 0; 
    foreach($topics as $key=>$value) { 
$index++; 
if($key != "") {  
      $trending[$key] = $value; 
      echo "<li><a class='size".$index."'  href='http://twitter.com/#!/search/realtime/%23".$key."'>#".$key."</a></li>"; 
    } 
    if($index >6) 
    break;  
} 
echo "</ul>"; 

?> 
+0

請加上'的var_dump($主題);'的multisort之前,向我們展示了輸出。 – 2011-12-25 10:19:55

+0

謝謝我想通了一個我的cron工作被打破了,哪種情況下沒有存檔,謝謝 – 2011-12-25 10:30:14

回答

-1

該錯誤可能是由於數據庫中沒有數據。 $row['hashtags']爲空。嘗試直接在sql服務器上執行查詢,看看是否有任何結果。

更新:我更新了你的代碼來處理錯誤:

<?php 

require_once("dbconnect.php"); 
$query = " SELECT COUNT(*) cnt, hashtags 
FROM tweets 
WHERE tweeted_at > DATE_SUB(NOW() , INTERVAL 1 DAY) 
AND hashtags != '' 
GROUP BY hashtags 
ORDER BY cnt DESC "; 
$res = mysql_query($query); 
$topics = array(); 
while($row = mysql_fetch_assoc($res)) { 
    $count = $row['cnt']; 
    $hashtags = explode(" ", $row['hashtags']); 
    if(!empty($hashtags)) 
    { 
     foreach($hashtags as $hashtag) { 
      //$index = array_search($hashtag, array_keys($topics)); 
      if(strtolower($hashtag) != 'gmaes' && strtolower($hashtag) != 'lord') 
       $topics[strtolower($hashtag)] += $count; 
     } 
    } 
} 
if(!empty($topics)) 
{ 
    array_multisort($topics, SORT_DESC); 

    echo "<ul id='mytags'>"; 
    $index = 0; 
    foreach($topics as $key=>$value) { 
     $index++; 
     if($key != "") { 
      $trending[$key] = $value; 
      echo "<li><a class='size".$index."'  href='http://twitter.com/#!/search/realtime/%23".$key."'>#".$key."</a></li>"; 
     } 
     if($index >6) 
      break; 
    } 
} 
+0

nope不是空的已經檢查DB之前發帖 – 2011-12-25 10:29:47

+0

@Virendra,你不可能先不問。所以這不是一個答案。 – 2011-12-25 10:32:13

+0

我已更新代碼以添加錯誤檢查。 好吧,那麼你可以檢查一下'var_dump($ topics)'並告訴我輸出結果嗎? – Virendra 2011-12-25 10:33:12