reorder sql query


Posted on 16th Feb 2014 07:03 pm by admin

ok so i have this very simple code:
Code: $query = "SELECT search_keywords, COUNT(search_keywords) AS counter FROM phpbb_popsearch GROUP BY search_keywords ORDER BY counter DESC limit 5";
$res = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($res)) {
$term = $row["search_keywords"];
echo $term;
}

phpbb_popsearch is a table where i put the last search terms used in my forum search engine

this code will group together the same terms and list the 5 most popular terms

my problem is that the results will be ordered by "counter" which is the number of time a term has been found.

I want to get the 5 most popular results AND THEN, AFTER, list the results alphabetically

if i sort the results alphabetically right in the sql query (replacing "ORDER BY counter" by "ORDER BY search_keywords") it will not work correctly because it will not get the 5 most popular terms but just the 5 first terms, alphabetically sorting them and ignoring the number of times the term has been found

thanks for help

No comments posted yet

Your Answer:

Login to answer
135 Like 10 Dislike
Previous forums Next forums
Other forums

Escaped characters
I have a script that allows you to post news to the home page of my site. Along with the news is the

login from external site
Hi my new experience begins, Now what i am trying to do is i make three pages, login.php logout.php

How to show next and prev records
Hi all,

Sorry if this is simple, i'm very new to php, well, any programming language actually

Search in the PHP Files
have to search asterisk(*) in the php code of the php files .
In the html page one text box

DataTable Help Required
Hi all I am creating an app that utilises and MS Access back end and queries it quite a bit at vario

Sharing PHP Sessions Across Domains
I am in the process of writing a script to share a php session across various domains I have.
The

Encrypt php code?
Is it possible to encrypt php code in files,
so that it displays a load of unreadable characters

Image upload - determine folder
I have an upload script that may be accessed from different directories. Problem is, the script is m

Required to login help
I'm trying to set up my site so users have to be logged into the forum to access the site.I've been

using variables in another page
I have a test database set up on localhost. I have a form that I can type a name into, hit the butto

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash