Php shopping cart code

Source: Internet
Author: User

Here we provide you with a simple php shopping cart code. from adding shopping products and purchasing products, this function is indispensable in mall development and we do not need a database, A txt file is used to operate the user's shopping content.

Here we provide you with a simple php tutorial shopping cart code. from adding shopping products and purchasing, this function is indispensable in mall development. We do not need database tutorials, A txt file is used to operate the user's shopping content.

Add item to shopping cart

<? 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 the 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 "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];
 
}
?>

<Html>
<Head>
<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>
</Head>
<Body>
<? Php if ($ _ POST [ordered]) {?>
<H1> <? Php echo $ curr_product [name];?>
Successfully added to the shopping basket

<A href = "cart. php"> return to the </a> item list page.
<? Php} else {?>
<H1> Add <? Php echo $ curr_product [name];?> To your shopping basket

<Form action = "<? Php echo $ PHP_SELF;?> "Method =" post ">
Product Name: <? Php echo $ curr_product [name];?>
<Br>
Product Description: <? Php echo $ curr_product [desc];?>
<Br>
Unit Price: RMB <? Php echo $ curr_product [price];?>
<Br>
Item quantity: <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>
</Html>

View items in the shopping cart

<? Php
//
// Cart. php: www.bkjia.com
//
Session_start ();

Require 'lib. inc. php ';
// Determine whether cart is registered as the shopping basket session variable. If cart is not registered, the cart variable is registered.
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 (); // load the item list
?>

<Html>
<Head>
<Title> Demonstrate the Shopping Basket Program for session tracking </title>
</Head>

<Body>

<H1> welcome to the online store

<? Php
If ($ _ SESSION [cart] [num_items]) {// If there is something to show
?>
<H2> items in the current shopping basket <Br>
<Table border = "2" cellpadding = "5" cellspacing = "2">
<Tr>
<Th>
Product Name
</Th>
<Th>
Product Description
</Th>
<Th>
Unit Price
</Th>
<Th>
Quantity
</Th>
<Th> & nbsp;

</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>
<Td colspan = "2" ALIGN = "right">
<B> total: </B>
</Td>
<Td colspan = "2">
RMB: <? Php echo $ total;?>
</Td>
<Td> & nbsp; </td>
</Tr>
</Table>
<Br>
<Br>
<? Php
}
?>

<H2> items to be sold in the store <Br>
<I>
We provide the following products for sale:
</I>
<Br>
<Table border = "2" cellpadding = "5" cellspacing = "2">
<Tr>
<Th>
Product Name
</Th>
<Th>
Product Description
</Th>
<Th>
Unit Price
</Th>
<Th> & nbsp;

</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>

Modify the number of cart

<? Php
//
// Change_quant.php:
// Change the quantity of an item in the shopping cart.
//
Session_start ();
If (session_is_registered ('cart ')){
Session_register ('cart ');
}

// Typecast to int, making sure we access
// 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 );
?>

<Html>
<Head>
<Title>
Quantity Modification
</Title>
</Head>
<Body>
<H1> count: <? Php echo $ old_num;?> Change
<? Php echo $ _ POST [quantity];?> </H1>
<A href = "cart. php"> return to the </a> item list page.
</Body>
</Html>

Function page, the user saves the content in the shopping cart to the txt Database

<? Php
// Item Array
$ Master_products_list = array ();


// Function for loading item data
Function LoadProducts (){
Global $ master_products_list;
$ Filename = 'products.txt ';

$ Fp = @ fopen ($ filename, "r ")
Or die ("opening the $ filename file failed ");
@ Flock ($ fp, 1)
Or die ("failed to lock the $ filename file ");

// Read the file content
While ($ line = fgets ($ fp, 1024 )){
List ($ id, $ name, $ desc, $ price) = explode ('|', $ line); // read data in each row, and use |
$ Id = trim ($ id); // remove the first and last special characters
$ Master_products_list [$ id] = array ("name" => $ name, // name
"Desc" => $ desc, // description
"Price" => $ price); // unit price
}

@ Fclose ($ fp) // close the file
Or die ("failed to close the $ filename file ");
}
?>

It's easy. We only use four files to implement the shopping cart function in php. Well, it's just a simple one.Php shopping cart codeFor more complex scenarios, consider more and better.


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.