计数后对结果进行排序 PHP
该代码效果很好,但我不知道如何订购它。
示例输出:
点击 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 SQL 调整为
您只需要查询一次,然后迭代结果
Tweak the SQL to
And you just need to query once, then iterate the results
尝试排序如下:
将它们放在一起
这应该可以满足您的要求......
Try ordering something like:
Putting it all together
That should do what you are looking for...