0

I'm trying to get the rank from the total quantity an item has sold so far I can get the total quantity an item has sold. But I dont no how to get the rank of the items ranking number 1 to the item that sold the most, Can some one please help me figure this out below is the working code that I have so far that gets the total amount of each item. Below is my PHP & MySQL code.

PHP & MySQL code

function rank(){
    $query = "SELECT SUM(`orders`.`quantity`) AS `total`, `orders`.`product_id`, `data`.`member_id`
              FROM `orders`
              INNER JOIN `data` ON `orders`.`product_id` = `data`.`product_id`
              GROUP BY `orders`.`product_id`
              ORDER BY `total` DESC";
    //short hand if statement ? if-true : if-false to determine the number of results returned by the query                                          
    $count = ($query = mysqli_query(database(), $query)) ? mysqli_num_rows($query) : 0; 

    if($count === 0){
        return false;       
    } else {
        while($row = mysqli_fetch_assoc($query)){
            $results[] = array(
                         'total' => $row['total'],
                         'member_id' => $row['member_id'],
                         'product_id' => $row['product_id']
            );
        }

        return $results;
    }   
}
juice.gin
  • 71
  • 9

0 Answers0