PHP Inventory Lesson โ€” 1-Page Highlighter Dissection

The 3-Station Kitchen Model: 1. Take the Order Ticket ($_POST) → 2. Pull Stock from Database (JSON) → 3. Plate the HTML Table (foreach)
Illustrative PHP excerpt (38 Lines)
VIEW: COMPLETED 4-COLOR KEY

Read the mechanics first: PHP reads input, calculates a preview, fetches inventory JSON, then renders HTML. This printed example does not execute or reserve stock. Validation and API failure handling are omitted; price rules are for this lab only.

๐ŸŸจ 1. YELLOW: Variables ($) Highlight $sku, $sqft, $sqyd, $products, $p. PHP variable names start with $.
๐ŸŸฉ 2. GREEN: Web Input ($_POST) Highlight $_SERVER['REQUEST_METHOD'] and $_POST[...]. The order ticket sent from the customer's browser form.
๐ŸŸง 3. ORANGE: Flooring Rules Highlight ceil($sqft / 9.0) and if ($p['requiresQuote']). Converts SqFt → SqYd and follows a demo price rule.
๐ŸŸฆ 4. BLUE: Loop & Safe Print Highlight foreach ($products as $p), <?=, and htmlspecialchars(). Loops over rows and escapes HTML text.
STATION 1 (Lines 01โ€“12): TAKE THE CUSTOMER TICKET & CONVERT SQFT → SQYD Kitchen Analogy: The Ticket Printer at the Pass
01<?php 02$apiUrl = "http://warehouse-api:5105"; 03$message = ""; 04 05if ($_SERVER['REQUEST_METHOD'] === 'POST') { 06 $sku = trim($_POST['sku'] ?? ''); 07 $sqft = (int)($_POST['sqft'] ?? 0); 08 $sqyd = (int)ceil($sqft / 9.0); 09 10 // Preview only: no reservation request is sent. 11 $message = "Preview: $sqyd SqYd ($sqft SqFt) of $sku"; 12}
  • Line 02โ€“03 (Yellow): Creates two containers in RAM: $apiUrl and an empty $message.
  • Line 05 (Green): Checks if the customer submitted the form via HTTP POST.
  • Lines 06โ€“07 (Green → Yellow): Pulls raw customer input out of $_POST['sku'] and $_POST['sqft'] and stores them in $sku and $sqft.
  • Line 08 (Orange): Divides Square Feet by 9 and uses ceil() to round UP to the next full Square Yard.
STATION 2 (Lines 13โ€“18): FETCH LIVE INVENTORY FROM SQL SERVER / C# API Kitchen Analogy: Checking the Walk-In Cooler Clipboard
13// Ask the C# .NET 10 API for the SQL Server inventory 14$rawJson = file_get_contents("$apiUrl/api/inventory"); 15 16// Decode the JSON text string into a PHP Array of rows 17$products = json_decode($rawJson, true) ?? []; 18?>
  • Line 14 (Orange → Yellow): Calls the backend API and stores the raw JSON text string inside $rawJson.
  • Line 17 (Orange → Yellow): json_decode(..., true) unpacks the JSON text into a PHP list of rows named $products.
  • Line 18 (?>): Closes the top PHP prep block so we can write standard HTML below.
STATION 3 (Lines 19โ€“38): LOOP OVER ROWS & DISPLAY THE APIโ€™S PUBLIC PRICE Kitchen Analogy: Plating Each Dish for the Dining Room
19<h2>Dalton Carpet Inventory</h2> 20<p><?= htmlspecialchars($message) ?></p> 21 22<table border="1"> 23 <tr><th>SKU</th><th>Name</th><th>Price</th><th>Stock</th></tr> 24 <?php foreach ($products as $p): ?> 25 <tr> 26 <td><?= htmlspecialchars($p['sku']) ?></td> 27 <td><?= htmlspecialchars($p['productName']) ?></td> 28 <td> 29 <?php if ($p['requiresQuote']): ?> 30 <strong>Price withheld (demo rule)</strong> 31 <?php else: ?> 32 $<?= number_format($p['publicPrice'], 2) ?> 33 <?php endif; ?> 34 </td> 35 <td><?= (int)$p['availableToSell'] ?> units</td> 36 </tr> 37 <?php endforeach; ?> 38</table>
  • Line 24 & 37 (Blue): foreach ($products as $p) loops through the database array one row at a time, naming the current row $p.
  • Lines 26โ€“27 (Blue): <?= stamps the value into HTML, and htmlspecialchars() escapes special characters for this HTML text context.
  • Lines 29โ€“33 (Orange): The API decides which price can be public. PHP shows its publicPrice or a withheld-price label. This sends no quote.
๐ŸŽฏ Wife's 6-Question Highlighter Quiz (Have Her Ask You These 6 Questions in Any Order!) Point to the Color & Line #
Q1 (Green): "How does PHP know the customer clicked Submit instead of just loading the page?"
→ Line 05: It checks $_SERVER['REQUEST_METHOD'] === 'POST', and reads form fields from $_POST[...] on Lines 06โ€“07.
Q2 (Orange): "A customer measures their room at 250 SqFt. How does Line 08 calculate carpet yardage?"
→ Line 08: ceil($sqft / 9.0) divides by 9 (3 ft ร— 3 ft = 9 SqFt) and rounds UP to 28 SqYd so they aren't short.
Q3 (Yellow): "How does this PHP page get the inventory list from the backend database?"
→ Lines 14 & 17: file_get_contents() fetches the JSON string into $rawJson, and json_decode() turns it into the $products array.
Q4 (Blue): "If the database returns 500 rolls of carpet, how does PHP build 500 table rows in only 14 lines?"
→ Lines 24 & 37: The foreach ($products as $p) loop repeats the single <tr>...</tr> stencil once for every row.
Q5 (Orange): "How does PHP follow the APIโ€™s demo price decision?"
→ Lines 29โ€“33: Before printing a public price, if ($p['requiresQuote']) shows a withheld-price label. This does not establish manufacturer permission.
Q6 (Blue): "Why wrap raw product or user fields in htmlspecialchars() on Lines 20, 26, and 27?"
→ Lines 26โ€“27: Escapes this HTML text context. If a raw input field contained <script>, it renders as harmless text.