Problem in Sorting two resultset..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ashishhbti
Forum Newbie
Posts: 3
Joined: Mon Oct 08, 2007 2:49 am

Problem in Sorting two resultset..

Post by ashishhbti »

I ave a problem in sorting two resultset..
Supoose we have a follwing field in database..

id name vote type

1 ashish 2 1
2 ashish 4 2

i want to select top 5 records(according to vote) of type 1 and top voted 5 records of type 2 ....
then want to show on page these 10 records on sorting order according to vote ..

any suggestion ??

Please help me...

Thanks
Ashish...
User avatar
N1gel
Forum Commoner
Posts: 95
Joined: Sun Apr 30, 2006 12:01 pm

2 Queries

Post by N1gel »

Prehaps do it with 2 different queries and the join the results using php.

Code: Select all

$result1 = mysql_query('select name, votes from table where type = 1 order by votes desc limit 5');

$result2 = mysql_query('select name, votes from table where type = 2 order by votes desc limit 5');

$row1 = mysql_fetch_row($result1);
$row2 = mysql_fetch_row($result2);

$index1=0;
$index2=0;

while($index1 != 4 and $index2 != 4)
{
    if($row1[1] > $row2[1])
    {
        //Print $row1[0] the name
        $row1 = mysql_fetch_row($result1);
        $index1++;
    }
    else
    {
        //Print $row2[0] the name
        $row2 = mysql_fetch_row($result2);
        $index2++;
    }
}
This is just an example and i haven't tested any of the code but hopefully it gives you an idea for the solution :D
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I would probably use a single query.. a UNION.
ashishhbti
Forum Newbie
Posts: 3
Joined: Mon Oct 08, 2007 2:49 am

Post by ashishhbti »

Problem Solved..:)
Thanks N1gel.
@feyd..
it would better if you can provied single query.

Thanks to all.

Ashish.
Last edited by ashishhbti on Wed Oct 10, 2007 1:00 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

ashishhbti wrote:@fyed..
It's feyd. Common mistake.
ashishhbti wrote: it would better if you can provied single query.
This is a teaching site. I will not spoon feed you an answer.
ashishhbti
Forum Newbie
Posts: 3
Joined: Mon Oct 08, 2007 2:49 am

Post by ashishhbti »

feyd wrote:It's feyd..


now is it ok ??:)
happy ...
anything [s]4[/s] for [s]u[/s] you sir 8).
Problem solved ...so [s]u[/s] you no need to worry ..
i just said it would be better ...

[s]btw[/s] by the way Thanks..
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:11. Please use proper, complete spelling when posting in the forums. AOL Speak, leet speak and other abbreviated wording can confuse those that are trying to help you (or those that you are trying to help). Please keep in mind that there are many people from many countries that use our forums to read, post and learn. They do not always speak English as well as some of us, nor do they know these aberrant abbreviations. Therefore, use as few abbreviations as possible, especially when using such simple words.

Some examples of what not to do are ne1, any1 (anyone); u (you); ur (your or you're); 2 (to too); prolly (probably); afaik (as far as I know); etc.
Post Reply