如何在php中显示项目的数量

I have multiple purchasable digital items which are in a database however they are grouped by numbers.

E.g. 5 records with the category as digital.

I have created a basic shop page which displays all of the records however I would like it so it will display the quantity of digital rather than display all of the records which the category as digital.

I currently have the following which displays all of the records but doesn't display the quantity:

$stmt = $user->runQuery("SELECT * FROM `items` WHERE `purchased_by` = 0 AND `type` = 'digital' AND `city` NOT IN ('London') AND `level` NOT IN ('PLATINUM', 'GOLD') ORDER BY `id` ASC");
$stmt->execute();
while ($getDetails = $stmt -> fetch(PDO::FETCH_ASSOC)){
    $id = $getDetails['id'];
    $level = $getDetails['level'];
    $price = $getDetails['price'];
        echo '<tr>
              <td>'.htmlspecialchars($level).'</td>
              <td><form method="post">
        <button class="btn btn-success" value="'.$id.'" id="buyNow" type="submit" name="buyNow"> Buy Now - £'.htmlspecialchars($price).'</button>
        </td></form>
              </tr>';
}

I'd rather have something similar like this so it displays quantity next to the level which is x5 rather than displaying 5 records.

echo '<tr>
                  <td>'.htmlspecialchars($level).'</td>
                  <td>'.$quantity.'</td>
                  <td><form method="post">
            <button class="btn btn-success" value="'.$id.'" id="buyNow" type="submit" name="buyNow"> Buy Now - £'.htmlspecialchars($price).'</button>
            </td></form>
                  </tr>'

I was thinking using foreach however not sur ehow I would do it as it uses arrays.

  1. Create counter which will be incremented each loop run:

    $counter = 0; while ($getDetails = $stmt -> fetch(PDO::FETCH_ASSOC)){ $counter++; ...

  2. You can use database COUNT function.

Can you show the database structure? This table have not any relations?

The query needs to be updated to: $stmt = $user->runQuery("SELECT *, count(id) AS count FROM items WHERE purchased_by = 0 AND type = 'digital' AND city NOT IN ('London') AND level NOT IN ('PLATINUM', 'GOLD') GROUP BY bin ASC");

Then you can access the count by $getDetails['count'];