PHP Shopping Cart Implementation Method _php skills

Source: Internet
Author: User
Tags require

This article illustrates the implementation method of the PHP shopping cart. Share to everyone for your reference. The specific analysis is as follows:

Here we provide you with a simple PHP shopping cart code, from the increase in shopping products and purchase, in the mall development, this function is indispensable, we do not need a database, using a txt text file to operate the user shopping content.

To add merchandise to the shopping cart, the code is as follows:

Copy Code code as follows:
<?php
//
add_item.php:
Add an item to the shopping cart.
//
Session_Start ();
if (session_is_registered (' cart ')) {
Session_register (' cart ');
}

Require ' lib.inc.php '; Loadproducts ()

Loadproducts (); Load Products in $master _products_list

Make $curr _product Global
$curr _product = Array ();

Loop through all products and pull up the product
That we are interested in

foreach ($master _products_list as $prod _id => $product) {
if (Trim ($prod _id) = = Trim ($_get[id]) {
$curr _product = $product;
}
}

Register our session
Session_register (' cart ');
if (session_is_registered (' cart ')) echo "has been registered";

if ($_post[ordered]) {//If they have chosen the product

Array_push ($_session[cart][products], array (Trim ($_post[id)), $_post[quantity]);
$_session[cart][num_items] + = $_post[quantity];
}
?>
<title>
<?php if ($_post[ordered]) {?>
Added <?php Echo $curr _product[name];?> to your shopping basket
<?php} else {?>
Add <?php echo $curr _product[name];?> to your shopping basket
<?php}?>
</title>
<body>
<?php if ($_post[ordered]) {?>
Add to Basket Success
<a href= "cart.php" > Return </a> Product List page.
<?php} else {?>

<form action= "<?php echo $PHP _self;?>" method= "POST" >
Product Name: <?php echo $curr _product[name];?>
<br>
Product Description: <?php echo $curr _product[desc];?>
<br>
Commodity price: rmb<?php echo $curr _product[price];?>
<br>
Number of items: <input type= "Text" size= "7" name= "Quantity" >
<input type= "hidden" name= "id" value= "<?php Echo $_get[id];?>" >
<input type= "hidden" name= "ordered" value= "1" >
<input type= "Submit" value= "Add to Shopping Bar" >
</form>
<?php}?>
</body>

To view the merchandise for your shopping cart, the code is as follows:

Copy Code code as follows:
<?php
//
cart.php:
//
Session_Start ();

Require ' lib.inc.php ';
To determine whether the basket Session variable cart is registered or not, register the cart variable
if (session_is_registered (' cart ')) {
Session_register (' cart ');
}

If the shopping basket is not initialized, the shopping basket is initialized
if (!isset ($_session[cart][num_items])) {
$_session[cart] = Array ("Num_items" => 0,
"Products" => array ());
}
From Site_lib.inc, loads the $master _products_list array
Loadproducts (); Loading Items List
?>
<title> demo session Tracking shopping Basket program </title>

<body>


<?php
if ($_session[cart][num_items]) {//If there is something to show
?>
<br>
<table border= "2" cellpadding= "5" cellspacing= "2" >
<tr>
<th>
Product Name
</th>
<th>
Product Description
</th>
<th>
Price
</th>
<th>
Number
</th>
<th>

</th>
</tr>
<?php

Loop through the Products
foreach ($_session[cart][products] as $i => $product) {
$product _id = $product [0];
$quantity = $product [1];

$total + + $quantity *
(double) $master _products_list[$product _id][price];
?>
<tr>
<td>
<?php echo $master _products_list[$product _id][name];?>
</td>
<td>
<?php echo $master _products_list[$product _id][desc];?>
</td>
<td>
<?php echo $master _products_list[$product _id][price];?>
</td>
<td>
<form action= "change_quant.php" method= "POST" >
<input type= "hidden" name= "id" value= "<?php echo $i;?>" >
<input type= "Text" size= "3" name= "Quantity"
Value= "<?php echo $quantity;?>" >
</td>
<td>
<input type= "Submit" value= "Quantity Change" >
</form>
</td>
</tr>
<?php
}
?>
<tr>
&LT;TD colspan= "2" align= "right" >
<b> Total: </b>
</td>
&LT;TD colspan= "2" >
rmb:<?php echo $total;?>
</td>
<td> </td>
</tr>
</table>
<br>
<br>
<?php
}
?>

<br>
<i>
We offer the following items for sale:
</i>
<br>
<table border= "2" cellpadding= "5" cellspacing= "2" >
<tr>
<th>
Product Name
</th>
<th>
Product Description
</th>
<th>
Price
</th>
<th>

</th>
</tr>
<?php
Show all of the products
foreach ($master _products_list as $product _id => $item) {
?>
<tr>
<td>
<?php echo $item [name];?>
</td>
<td>
<?php echo $item [desc];?>
</td>
<td>
$<?php echo $item [price];?>
</td>
<td>
<a href= "add_item.php?id=<?php echo $product _id;?>" >
Add to Shopping basket
</a>
</td>
</tr>
<?php
}

?>
</table>

To modify the number of shopping carts, the code is as follows:

Copy Code code as follows:
<?php
//
change_quant.php:
Change the quantity of a item in the shopping cart.
//
Session_Start ();
if (session_is_registered (' cart ')) {
Session_register (' cart ');
}

Typecast to int, making sure we access the
Right element below
$i = (int) $_post[id];

Save the old number of products for display
and arithmetic
$old _num = $_session[cart][products][$i][1];

if ($_post[quantity]) {
$_session[cart][products][$i][1] = $_post[quantity]; Change the quantity
} else {
Unset ($_session[cart][products][$i]); Send the product into oblivion
}

Update the number of items
$_session[cart][num_items] = ($old _num >$_post[quantity])?
$_session[cart][num_items]-($old _num-$_post[quantity]):
$_session[cart][num_items] + ($_post[quantity]-$old _num);
?>

<title>
Quantity modification
</title>
<body>
<?php Echo $_post[quantity];?><a href= "cart.php" > Return </a> Product List page.
</body>

Function page, the user saves the contents of the shopping cart to the TXT database, the code is as follows:

Copy Code code as follows:
<?php
Items array
$master _products_list = Array ();


Loading Item data function
function Loadproducts () {
Global $master _products_list;
$filename = ' products.txt ';

$fp = @fopen ($filename, "R")
Or Die ("Open $filename file failed");
@flock ($FP, 1)
Or Die ("Lock $filename file failed");

Read File contents
while ($line = fgets ($fp, 1024)) {
List ($id, $name, $desc, $price) = Explode (' | ', $line); Read each row of data, the data to | GE Kai
$id = Trim ($id); Remove the end and end special symbols
$master _products_list[$id] = Array ("name" => $name,//Name
"desc" => $desc,//description
"Price" => $price); Price
}

@fclose ($FP)//close file
Or Die ("Shutdown $filename file failed");
}
?>

Very simple, we only use 4 files to achieve the use of PHP shopping cart function, okay this is just a simple PHP shopping cart code more complex need to consider more and better.

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.