When people are trying to help you it is impolite to be rude.
This is the best description you’ve given of your situation so far. You’re describing a M<->N or “many to many” relationship between two entities. This will require 3 database tables, not one. And get your head off the excel spreadsheets - with this problem you have to get out of the tee-ball league.
The products table at a minimum has fields for id and name, and could also have fields for manufacturer, SKU code, and so on.
The sizes table has at a minimum fields for id and name as well (large, extra-large, etc).
Note neither table has a quantity field, nor should it.
A third table will describe the relationship between these two entities, so it will have at a minimum 4 fields: an id (never create any table without a primary key), a product_id (foreign key to the product table), a size_id (foreign key to the size table) and quantity.
The situation your describing, a report of the products and available sizes, could be retrieved from the database in this fashion
SELECT
`products`.`name` as `product_name`,
`sizes`.`name` as `size_name`,
`products_sizes`.`quantity` as `quantity`
FROM `products_sizes`
JOIN `products` ON `products_sizes`.`product_id` = `products`.`id`
JOIN `sizes` ON `products_sizes`.`size_id` = `sizes`.`id`
WHERE `products_sizes`.`quantity` > 0
ORDER BY `products`.`name` ASC, `sizes`.`name` ASC
This would give you this array structure for each row.
[
0 => [
'product_name' => 'Bonzai T-Shirt',
'size_name' => 'Small'
'quantity' => 25
]
]
If you have 10 products and 10 sizes you can have up to 100 rows on the product_size table, but you will only get pairings that have an entry. If a product has a quantity changed to 0 then that’s what the where clause in the query above was for - to exclude such rows.
Building the grid would require you to issue a query to get all product names
$products = $pdo->query('SELECT `id`, `name` FROM `products`')
->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_UNIQUE|PDO::FETCH_COLUMN);
And a query for the sizes.
$sizes = $pdo->query('SELECT `id`, `name` FROM `sizes`')
->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_UNIQUE|PDO::FETCH_COLUMN);
And a finally query for the quantities
$quantities = $pdo->query('SELECT `id`, `product_id`, `size_id`, `quantity` FROM `products_sizes`')
->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_UNIQUE|PDO::FETCH_ASSOC);
The database doesn’t return this information in a manner we can quickly map, so we need to do a bit of collation.
$quantity = [];
foreach ($quantities as $q) {
$quantity[$q['product_id']][$q['size_id']] = $q['quantity'];
}
Now, with these three in hand we iterate, the example below having the products on the headings. Reversing it is left as an exercise to the reader.
<table>
<thead>
<tr>
<th> </th>
<?php foreach ($products as $product): ?>
<th><?= $product ?></th>
<?php endforeach ?>
</tr>
</thead>
<tbody>
<?php foreach ($sizes as $size => $size_name): ?>
<tr>
<th><?= $size_name ?></th>
<?php foreach (array_keys($products) as $product): ?>
<td><?= isset($quantity[$product][$size]) ? $quantity[$product][$size] : 0 ?></td>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</tbody>
</table>
Transforming the above into a form is also left to the reader.
Incidentally, with this db structure retrieving a total number of products would use this SQL
SELECT
`products`.`id` as `id`,
`products`.`name` as `name`,
SUM(`products_sizes`.`quantity`) as `quantity`
FROM `products`
JOIN `products_sizes` ON `products`.`id` = `products_sizes`.`product_id`
GROUP BY `products`.`id`
Sizes would use a similar query. This should wrap this up.