这是我得到的代码,让我们看看它是否适合你。
这是在“index.php”页面上
<?php
/* Author: Rick Kazman
* Created: 11/1/2008
* Modified: 10/28/2013 to allow data file to be stored in current folder
* Description: This program displays a table of products and allows products to be added to
* the online store. The new product contains a product number, description, image, and price.
*/
include "functions.inc";
// Useful variables
$datafile = "products.dat";
if (!isset($errors)) $errors = array();
/********************************************
* Populate and update the array of products.
********************************************/
if (array_key_exists("add_item", $_POST))
{
// If the user has added an item, validate it. If valid, add it to the array
$products = arrayfile_to_array($datafile);
$new_item = $_POST['new_item'];
$errors = validate_price($errors, $new_item['price']);
$errors = validate_product($errors, $new_item['prodNum'], $products);
if (count($errors) == 0) // Only add the item if there are no errors
{
$products[] = $new_item;
unset($_POST['new_item']);
}
array_to_arrayfile($products, $datafile); // Rewrite the data file
}
else if (array_key_exists("delete_item", $_POST))
{
// If the user has added an item, add it to the array and save the file
$products = arrayfile_to_array($datafile);
$selected = $_POST['to_del'];
$size = count($products);
// Find all the products selected for deletion and remove them
for ($i=0; $i < $size; $i++)
{
if (isset($selected[$i]) && $selected[$i] == "on") unset($products[$i]);
}
$products = array_values($products); // Reorder the array
array_to_arrayfile($products, $datafile); // Rewrite the array file
}
else
{
if (!file_exists($datafile)) // Data file doesn't exist, so create it
{
/* $product1 = array('prodNum' => 'P1221',
'description' => 'Double Bubble Windscreen',
'image' => 'https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcT9B6kGG3G1q1Ag1bqPI0ERrNgCaO8aeQxOhvtuStpNDBs-8smK',
'price' => 50);
$product2 = array('prodNum' => 'P1346',
'description' => 'Michelin Pilot Power 2ct Set',
'image' => 'http://www.motorcycletoystore.com/sport/images/uploads/power2t.jpg',
'price' => 300);
$product3 = array('prodNum' => 'P2094',
'description' => 'Scotts Steering Damper',
'image' => 'https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcTwTD43wbFLr9vCP36C1ANaq-6bSI3XoU9N8IIUdkbGVw1pz7_UBA',
'price' => 350);
$product4 = array('prodNum' => 'P3942',
'description' => 'Frame Slider',
'image' => 'http://fstg.motosport.com/motographics/images/products/Metric/07body/LP_CarbonInlaySlider.jpg',
'price' => 100);
$product5 = array('prodNum' => 'P8055',
'description' => 'Yoshimura Exhaust',
'image' => 'http://www.motorcycle-superstore.com/ProductImages/300/0000_Yoshimura_TRC_Slip-On_Exhaust.jpg',
'price' => 850);
$products = array($product1, $product2, $product3, $product4, $product5); */
$products = array();
array_to_arrayfile($products, $datafile);
}
else
{
$products = arrayfile_to_array($datafile);
}
}
/***************************
* Display the product array
***************************/
?>
<form method='POST' action='<?php $_SERVER["PHP_SELF"] ?>'>
<h1>Welcome to the MotoStuffe Shoppe!</h1>
<table border =3>
<tr><td><b>Delete?</b></td>
<td><b><center>Product#</center></b></td>
<td><b><center>Product</center></b></td>
<td><b><center>Description</center></b></td>
<td><b><center>Price</center></b></td><tr>
<?php
foreach ($products as $key => $product)
{
// Print each table row: Product#, Product image, Description, Price
$prodNum = $product['prodNum'];
$image = $product['image'];
$price = sprintf("$%.2f", $product['price']);
$desc = $product['description'];
echo "<tr><td><center><input type=Checkbox name=to_del[$key]></center></td><td><center>$prodNum</center></td>" .
"<td><center><img height=150 src=$image></center></td><td><center>$desc</center></td><td><center>$price</center></td></tr>";
}
?>
</table>
<!-- Get user input / report errors / make forms "sticky" -->
<table>
<tr><td>Product#:</td><td><input type='text' name='new_item[prodNum]'
value= "<?php if(isset($_POST['new_item'])) {$tmp = $_POST['new_item']; echo $tmp['prodNum']; } ?>" >
<?php if (isset($errors['product'])) echo " <font color='red'>{$errors['product']}</font>"; ?></td></tr>
<tr><td>Image:</td><td><input type='text' name='new_item[image]'
value= "<?php if(isset($_POST['new_item'])) {$tmp = $_POST['new_item']; echo $tmp['image']; } ?>" ></td></tr>
<tr><td>Description:</td><td><input type='text' name='new_item[description]'
value= "<?php if(isset($_POST['new_item'])) {$tmp = $_POST['new_item']; echo $tmp['description']; } ?>" ></td></tr>
<tr><td>Price:</td><td><input type='text' name='new_item[price]'
value= "<?php if(isset($_POST['new_item'])) {$tmp = $_POST['new_item']; echo $tmp['price']; } ?>" >
<?php if (isset($errors['price'])) echo " <font color='red'>{$errors['price']}</font>"; ?></td></tr>
<tr><td><input type="submit" value="Delete Item(s)" name="delete_item"></td>
<td><input type="submit" value="Add Item" name="add_item"></td></tr>
</table></form>
<?php $errors = array(); // Clear the errors array
?>
这是在名为“functions.inc”的第二页上
<?php
// Useful functions
// Created by: Rick Kazman
// Creation date: October 31, 2013
function array_to_arrayfile($theArray, $filepath)
{
// This function serializes an array in $theArray and saves it
// in the file at $filepath. The file may then be converted
// back into an array using the arrayfile_to_array() function)
if ($fp = fopen($filepath, "w+"))
{
$encoded = serialize($theArray);
fwrite($fp, $encoded);
fclose($fp);
}
else
echo "Unable to write array to $filepath";
}
function arrayfile_to_array($filepath)
{
// This function reads the file at $filepath and returns an
// array. It is assumed that the file being read is a
// serialized array (created using array_to_arrayfile)†
$fsize = @filesize($filepath);
if ($fsize > 0)
{
$fp = fopen($filepath, "r");
$encoded = fread($fp, $fsize);
fclose($fp);
return unserialize($encoded);
}
else
echo "$filepath does not exist!";
}
function validate_price($errors, $value)
{
if( !is_numeric($value) || $value - round($value, 2) != 0 || $value <= 0 )
{
$errors['price'] = "Invalid price -- please re-enter";
}
return $errors;
}
function validate_product($errors, $prodID, $products)
{
foreach ($products as $key => $aProduct)
{
if ($prodID == $aProduct['prodNum']) // If the product ID already exists, reject it
{
$errors['product'] = "Invalid product ID -- please re-enter";
break;
}
}
return $errors;
}
?>