计数后对结果进行排序 PHP

发布于 2024-12-03 15:01:38 字数 1770 浏览 0 评论 0原文

该代码效果很好,但我不知道如何订购它。

示例输出:

点击 3 Mozilla Firefox
点击 2 Google Chrome
点击 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>';
}
}

This code works great but I'm lost on how to order it.

Example output:

Hits 3 Mozilla Firefox
Hits 2 Google Chrome
Hits 4 Internet Explorer
Hits 2 Apple Safari
Hits 1 Opera

$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>';
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剩余の解释 2024-12-10 15:01:38

将 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;

您只需要查询一次,然后迭代结果

Tweak the SQL to

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;

And you just need to query once, then iterate the results

流绪微梦 2024-12-10 15:01:38

尝试排序如下:

  • SUBSTRING_INDEX(colName,2) --> HITS 2 歌剧 ===>命中 2
  • SUBSTRING(,INSTR(' ',colName)) -->点击 2 ==> 2
  • 演员(已签名)-->将 2 转换为数字

将它们放在一起

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

这应该可以满足您的要求......

Try ordering something like:

  • SUBSTRING_INDEX(colName,2) --> HITS 2 Opera ===> HITS 2
  • SUBSTRING(,INSTR(' ',colName)) --> HITS 2 ==> 2
  • CAST( AS SIGNED) --> Convert 2 to number

Putting it all together

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

That should do what you are looking for...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文