12c05-2.php
来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 70 行
PHP
70 行
<?php// Our products page// We will display all the products and allow people to choose them:session_start();// Include the products library//require 'products.php';require '12c05-1.php';// If we were given an item to add to our cart, do so:if (isset($_GET['add'])) { @$_SESSION['cart'][$_GET['add']]++;}// Calculate the total items, and total value, of our cart:$total_num = 0;$total_value = 0;if (@is_array($_SESSION['cart'])) { foreach ($_SESSION['cart'] as $id => $count) { $total_value += $products[$id]['price'] * $count; $total_num += $count; }}?><!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>View Products</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><style>table { border-collapse: collapse; }.num { text-align: right; }#cart { float: right; text-align: center; }td, th, div { border: 1px solid black; padding: 4px;}.button { display: block; padding: 2px 5px; background-color: #0000FF; color: white; text-decoration: none; border-style: solid; border-width: 4px; border-bottom-color: #000099; border-right-color: #000099; border-top-color: #0066FF; border-left-color: #0066FF;}</style></head><body><div id="cart">Items in your cart: <?= $total_num ?><br />Total value of cart: <?= $total_value ?><br /><a href="12c05-3.php">View your cart</a><!--<br /><a href="cart.php">View your cart</a>--></div><p>Please choose what products you may desire below:</p><table> <tr><th scope="col">Item</th><th scope="col">Name</th> <th scope="col">Price</th><th scope="col">Buy it!</th></tr><?php// Loop through all the products and give the options:$counter = 0;foreach ($products as $id => $p) { // Echo out a table row with this data: $counter++; echo "<tr><td>{$counter}</td><td>{$p['desc']}</td><td class=\"num\">", number_format($p['price'],2), "</td>"; // Finish off the row by creating a 'Add to Cart' button ... echo "<td><a class=\"button\"href=\"{$_SERVER['PHP_SELF']}?add={$id}\">Add to Cart</a></td></tr>\n";}?></table></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?