12c05-3.php

来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 85 行

PHP
85
字号
<?php// The shopping cart page itselfsession_start();// Include the products library//require 'products.php';require '12c05-1.php';// If we were asked to clear the cart, do so immediately.if (@$_POST['submit'] == 'Clear Cart') {    unset($_SESSION['cart']);}// Otherwise if we have been presented with an update, handle it:elseif (isset($_POST['update'])) {    // Loop over all the updates    foreach ($_POST['update'] as $id => $val) {        // Only update if the value is numeric or blank - Otherwise ignore.        $val = trim($val);        if (preg_match('/^[0-9]*$/', $val)) {            // If the value is 0 or blank, remove it:            if ($val == 0) {                unset($_SESSION['cart'][$id]);            } else {                // Otherwise, just reset to the new number                $_SESSION['cart'][$id] = $val;            }        }    }}?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Shopping Cart</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><style>table { border-collapse: collapse; }.num { text-align: right; }td, th, div { border: 1px solid black; padding: 4px;}</style></head><body><p>The following is in your shopping cart:</p><form action="<?= $_SERVER['PHP_SELF'] ?>" method="post"><table> <tr><th scope="col">Item</th><th scope="col">Name</th> <th scope="col">Price</th><th scope="col">Qty</th></tr><?php// Loop through all of the current cart:$counter = 0;$total = 0;if (@is_array($_SESSION['cart'])) {    foreach ($_SESSION['cart'] as $id => $c) {        // Echo out a table row with this data:        $counter++;        echo "<tr><td>{$counter}</td><td>{$products[$id]['desc']}</td>",            "<td class=\"num\">", number_format($products[$id]['price'],2),            "</td><td><input type=\"text\" size=\"3\" class=\"num\" ",            "name=\"update[{$id}]\" value=\"{$c}\" /></td></tr>\n";        // Update our total        $total += $products[$id]['price'] * $c;    }}// Now echo out the 'total' line, and the 'update/buy options'.  // NOTE:  As it currently stands this Buy button doesn't do anything.  //  That will need implemented to your own personal system.?><tr class="num"><td colspan="3">Total:</td> <td><?= number_format($total,2) ?></td></tr><tr class="num"><td colspan="4"><!--<input type="button" value="Keep Shopping" onclick="window.location.href='view.php'" />--><input type="button" value="Keep Shopping" onclick="window.location.href='12c05-2.php'" /><input type="submit" name="submit" value="Update Quantities" /><input type="submit" name="submit" value="Clear Cart" onclick="return confirm('Are you sure you wish to empty your cart?')" /><input type="button" value="Buy" /></td></tr></table></form></body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?