PHP Total Quantity Price

User
{"ringside_adult":{"quantity":1,"price":26,"seating_area":"Ringside","ticket_type":"Adult"},"ringside_child":{"quantity":1,"price":18,"seating_area":"Ringside","ticket_type":"Child"},"ringside_senior":{"quantity":1,"price":18,"seating_area":"Ringside","ticket_type":"Senior"},"tiered_adult":{"quantity":1,"price":18,"seating_area":"Tiered","ticket_type":"Adult"}}
in php count quantity total
Assistant
To calculate the total quantity and total price from the given array in PHP, you can do the following:

### ✅ PHP Code:

$data = [
    "ringside_adult" => [
        "quantity" => 1,
        "price" => 26,
        "seating_area" => "Ringside",
        "ticket_type" => "Adult"
    ],
    "ringside_child" => [
        "quantity" => 1,
        "price" => 18,
        "seating_area" => "Ringside",
        "ticket_type" => "Child"
    ],
    "ringside_senior" => [
        "quantity" => 1,
        "price" => 18,
        "seating_area" => "Ringside",
        "ticket_type" => "Senior"
    ],
    "tiered_adult" => [
        "quantity" => 1,
        "price" => 18,
        "seating_area" => "Tiered",
        "ticket_type" => "Adult"
    ]
];

$total_quantity = 0;
$total_price = 0;

foreach ($data as $ticket) {
    $total_quantity += $ticket['quantity'];
    $total_price += $ticket['quantity'] * $ticket['price'];
}

echo "Total Quantity: " . $total_quantity . "<br>";
echo "Total Price: £" . $total_price;

### 🧾 Output:

Total Quantity: 4  
Total Price: £80

Let me know if you want to group the totals by seating area or ticket type.

User
above saved in col
Assistant
If the above data is **saved in a column** (for example, in a MySQL database column named `ticket_data` as a JSON string), and you want to parse it and compute **total quantity and total price** in PHP, here’s how you can handle it:

### ✅ Step-by-step PHP Code (assuming it's stored as JSON in a DB column):

// Example JSON from the database column
$ticket_data_json = '{"ringside_adult":{"quantity":1,"price":26,"seating_area":"Ringside","ticket_type":"Adult"},"ringside_child":{"quantity":1,"price":18,"seating_area":"Ringside","ticket_type":"Child"},"ringside_senior":{"quantity":1,"price":18,"seating_area":"Ringside","ticket_type":"Senior"},"tiered_adult":{"quantity":1,"price":18,"seating_area":"Tiered","ticket_type":"Adult"}}';

// Decode the JSON into an associative array
$ticket_data = json_decode($ticket_data_json, true);

$total_quantity = 0;
$total_price = 0;

foreach ($ticket_data as $ticket) {
    $total_quantity += $ticket['quantity'];
    $total_price += $ticket['quantity'] * $ticket['price'];
}

echo "Total Quantity: " . $total_quantity . "<br>";
echo "Total Price: £" . $total_price;

### 🧾 Output:

Total Quantity: 4  
Total Price: £80

Let me know if you need to do this for **multiple rows** from the database or save the total back to another column.

Leave a Comment

Your email address will not be published. Required fields are marked *