count row

Discussion in 'PHP' started by roice, Aug 16, 2010.

  1. #1
    Hello,
    I have table for sells, that include: id, user_id, date (linux time)
    each record in the DB is 1 sell
    I want to print the user_id of the 10 users who made the top sells (the onces that have the biggest number of row that include the date of this month)

    how can I do that using PHP and MYSQL?
     
    roice, Aug 16, 2010 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    select user_id from sells where year(date) = (select year(now())) and day(date) = (select day(now())) and month(date) = (select month(now())) order by salescolumn desc limit 10
     
    bartolay13, Aug 16, 2010 IP
  3. jpratama

    jpratama Member

    Messages:
    31
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #3
    Maybe you can try this. I didn't test the code but you grab the similar concept even adjust and improve it to your needs. Consult always with php.net/manual as your best advisor.
    <?php
    // Make a MySQL Connection first
    
    $query = "SELECT user_id, COUNT(user_id) as sold FROM sells GROUP BY user_id"; 
    $result = mysql_query($query) or die(mysql_error());
    
    $sells = array();
    while($row = mysql_fetch_array($result)){
    	$sells[' . $row['user_id'] . '] = $row['sold'];
    }
    
    $ranks = arsort($sells, SORT_NUMERIC); // Sorting from high to low value
    ?>
    PHP:
     
    jpratama, Aug 16, 2010 IP