2011-09-08 71 views
0

此代碼效果很好,但我在如何訂購時遺失了。在點數後對結果進行排序PHP

輸出示例:

撞擊3是Mozilla Firefox
撞擊2谷歌瀏覽器
撞擊4的Internet Explorer
撞擊2 Apple Safari中
命中1歌劇

$result = mysql_query("SELECT * FROM WEB_STATS WHERE real_user NOT LIKE 'Bot'"); 
$duplicates = array(); 
while($row = mysql_fetch_array($result)) { 
    $results = $row['user_browser']; 

if (!array_key_exists($results, $duplicates)) { 
    $browsersLive[] = $results; 
    $duplicates[$results] = 1; ## mark that we've already output this records 
    } 
    } 

foreach($browsersLive as $browserswive){ 
    $query = "SELECT COUNT(*) FROM WEB_STATS WHERE user_browser = '$browserswive'"; 
    $result = mysql_query($query) or die("Sorry, didn't work" . mysql_error()); 
    $browser_count = mysql_result($result, 0); 

    if($browserswive == "Mozilla Firefox"){ 
    echo '<img alt="FireFox" height="32" src="images/fox-icons.png" width="32">Hits '.$browser_count.' '.$browserswive.'<br>'; 
} 
    elseif($browserswive == "Internet Explorer"){ 
    echo '<img alt="Internet Explore" height="32" src="images/ie-icons.png" width="31">Hits '.$browser_count.' '.$browserswive.'<br>'; 
} 
    elseif($browserswive == "Google Chrome"){ 
    echo '<img alt="Google Chrome" height="32" src="images/chrome-icons.png" width="32">Hits '.$browser_count.' '.$browserswive.'<br>'; 
} 
    elseif($browserswive == "Apple Safari"){ 
    echo '<img alt="Apple Safari" height="32" src="images/safari-icons.png" width="32">Hits '.$browser_count.' '.$browserswive.'<br>'; 
} 
    elseif($browserswive == "Opera"){ 
    echo '<img alt="Opera " height="33" src="images/opera-icons.png" width="33">Hits '.$browser_count.' '.$browserswive.'<br>'; 
} 
} 
+0

什麼你怎麼想看看它命令?輸出示例是多列還是單個字符串? – Sparky

+0

我希望這些匹配按降序運行。上面的示例輸出是現在的顯示方式。 – MHowey

回答

1

歡迎調整SQL到

SELECT user_browser, COUNT(*) as hits 
FROM WEB_STATS 
WHERE real_user!='Bot'  <-- replace the NOT LIKE 'Bot' 
GROUP BY user_browser 
ORDER BY hits DESC; 

,你只需要查詢一次,然後遍歷結果

+0

感謝您的「real_user!='Bot'」,但我沒有**命中**來排序。如果我做了,我不需要數數。除非我錯過了什麼 – MHowey

+0

再次更新SQL – ajreal

0

嘗試訂購類似:

  • SUBSTRING_INDEX(COLNAME,2) - > HITS 2歌劇===> HITS 2
  • SUBSTRING(,INSTR(」」,COLNAME)) - > HITS 2 ==> 2
  • CAST(AS簽名) - >轉換爲2〜數

全部放在一起

ORDER BY CAST(substring(substring_index(colName,2),INSTR(' ',colName)) AS SIGNED) 

這應該做你正在尋找...