TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-27-2009, 02:12 AM   #1 (permalink)
The Wanderer
 
Join Date: Jan 2009
Posts: 8
Thanks: 0
ttmtake is on a distinguished road
Default How to get row number from 2 top numberd rows

Hi, i need little help with this.

I have mysql like this:

PHP Code:
id          browser            country
--------------------------------------
1            MSIE               Japan  
2            MSIE               Europe 
3            FIREFOX            Canada
4            MSIE               Canada 
5            FIREFOX            Europe 
6            MSIE               France 
7            MSIE               USA
8            FIREFOX            Europe 
9            MSIE               Dansk 

Now i need to get number of rows for 2 top used countys, in this case that is 1=Europe and 2=Canada, so need to get $europe = 3 rows AND $canada = 2 rows.

I cant just count rows becouse i dont know which country is in most rows.
ttmtake is offline  
Reply With Quote
Old 01-27-2009, 02:25 AM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Please try the following query. I hope this is something like what you're after!

sql Code:
SELECT
    country,
    COUNT(*) AS num_entries
FROM
    countries
GROUP BY
    country
ORDER BY
    num_entries DESC
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 01-27-2009, 03:50 AM   #3 (permalink)
The Wanderer
 
Join Date: Jan 2009
Posts: 8
Thanks: 0
ttmtake is on a distinguished road
Default

Thank you, it is what iam looking.
With this code i can get first 2 names of most used countrys:

PHP Code:
$sql=mysql_query("SELECT
    country,
    COUNT(*) AS num_entries
FROM
    countrys
GROUP BY
    country
ORDER BY
    num_entries DESC LIMIT 2"
);
    
while (
$row mysql_fetch_array($sql)){
echo 
$row['country'];

Now, how can i get row number where 1 country is used and where 2 country is used. Like Europe = 3 rows
ttmtake is offline  
Reply With Quote
Old 01-27-2009, 04:08 AM   #4 (permalink)
The Wanderer
 
Join Date: Jan 2009
Posts: 8
Thanks: 0
ttmtake is on a distinguished road
Default

I think i got something, but meybe thers better way.

PHP Code:
$sql=mysql_query("SELECT
    country,
    COUNT(*) AS num_entries
FROM
    countrys
GROUP BY
    country
ORDER BY
    num_entries DESC LIMIT 2"
);
    
while (
$row mysql_fetch_array($sql)){
$country $row['country'];
$numresults mysql_query("SELECT * FROM `countrys` WHERE country='$country'"); 
$num=mysql_num_rows($numresults);
echo 
$row['country'];
echo 
$num;

ttmtake is offline  
Reply With Quote
Old 01-27-2009, 10:42 PM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Hmm. I think the query I posted to you originally will do it for you. Try something like the following in your PHP code, after the query:

php Code:
while ($row = mysql_fetch_array($sql))
{
    printf("The country %s has %d entries",
    $row['country'], $row['num_entries']);  
}

Is this what you're after? It will display how many rows there are of that particular country, ordered with the most popular at the top.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 01-28-2009, 11:16 PM   #6 (permalink)
The Wanderer
 
Join Date: Jan 2009
Posts: 8
Thanks: 0
ttmtake is on a distinguished road
Default

Yes, thats it, i think i was try this after you first answer, hmm, butt now all work good. Thanks
ttmtake is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[PHP/MySQL] Function to return ALL selected rows maZtah General 7 10-22-2012 09:41 AM
Unique random number? code_junkie Advanced PHP Programming 4 12-21-2008 12:01 AM
MySQL Buckeling after a million rows? drewbee General 8 05-08-2008 07:41 PM
how to get the rows affected(not number of rows) kororo203 Absolute Beginners 2 04-23-2008 08:32 AM
[PHP/MySQL] Pulling Multiple Rows *Solved* Andrew General 3 10-19-2007 07:10 PM


All times are GMT. The time now is 02:12 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design