Session Introduction
If you haven't already implemented your own shopping basket in PHP, you should be able to create one after reading this article. Even you may have
With a shopping basket, some of the tips I've provided here may help you improve your system.
I will give you some hints on how to do it may be able to reduce the unlimited query on the shopping basket table, or reduce the infinite number of text file fills
Your file system, because the application will only do but not delete them.
If you are new to this kind of topic, don't be afraid. It's actually fairly simple and effective, and you only need a host that supports PHP4,
such as Notepad or VI HTML editor, you are precious a few kinds, and this small example.
Idea
In 1998, I had a friend who worked at a hardware store and asked me to write an online store for them. Need to be quick and simple, while also
To have online management. When I had a 39-degree high fever, I became interested in creating power, so I wrote that thing in Perl, using Minisql
As the back end. I inserted the Mini shopping basket I mentioned throughout the process.
This mini shopping basket is only a little different from the usual shopping cart, it will show you something on every page of your shopping cart and don't need to put in
Something after a jump back and forth in the shopping cart. This is the original intention of my writing this article.
Goal
People like to control, so why not give them some transparency so that they believe they have control? This mini shopping basket demonstrates a
The perfect way: it gives people the information they need, and saves them one click, close to the online store's 3-hit example.
So the goal is to display this mini shopping basket as part of your page at any time and provide a summary at the end to
Modify the items in the shopping basket, and then send an order at the end.
Requirements
You should know what a session is. If you don't know, here's a quick outline. There have been a lot of articles on this topic that you
You can find all the code examples you need. Open a session on your start page and use the
<?php
Session_Start ();
?>
This will create a session name and a session ID. You can now use the default session via a cookie based setting
Way to pass a session ID through a form, or as a method of attaching a variable to your link (get method). Don't forget
Remember to release session at the end. :)
Your product should have a name, price and unique ID so that it can be used in this example, if not, you may need to do a little
Modify. Here I use MySQL as the back end to create pages and other, in order to make shopping work this is not necessary. :)
You should know the array. Now if someone has time to use my code and create a class of it, I'm naturally thankful to get it.
Defined
To simplify this example, I used four separate arrays and some extra variables. Code can be optimized. It's easy to
Display ideas and read the code. You can use the class to do better, but I'm not quite sure whether I can save the object to the session. Yes
Would anyone like to comment on this?
We also need a project counter. Of course you can use the count () command to do it, I just think it's good enough to always know
Tao has a number of items, and it also provides a good counter for loops.
Job
Let's assume that there is a list of items in your HTML page
ID Name Price
1 Mouse 25.00 Add
2 Key 100.00 Add
3 Car 5000.00 Add
4 Game 25.00 Add
Add a link to your page
The ' Add ' field above should be a link to put the item in a mini shopping basket. Use $php_self to point this link to it from
Has. Then add the product information to it. Here is the example of product 1.
The name of the product can contain spaces, so put it at the end of the link. The Get method seems very picky about this.
Prepare a mini shopping basket
For code reuse, let's create an extra file minibasket.inc. I use the. inc extension to identify my included files.
The reason for using this external file is that although you will use the session variable to pass to the shopping basket, the code still needs to be valid. This file will
Contains code to display the mini shopping basket, and also packages to add functions to the item. The best place to implement it is to include <?php include ("Minibasket.inc");?> commands where you need them.
The logic of Minibasket.inc
Take some time to think about this. What does a mini basket look like, and what features does it need? The Mini shopping basket shown here should be
It should look like this:
# Name Price
1 Mouse 25.00
3 Game 75.00
Total 100.00
You can easily format this output by using an external style sheet. However, it should not be too big. This mini shopping basket is a
Information, however it should not be the focus of the page you are displaying.
The logic of this file is very simple.
Check to see if you need to add a new item.
If true, increase the project
In addition, it checks for duplicate records and updates the existing records by updating the quantity and price.
Fragment 1. Check for new items that will be added
This is a normal if statement that is used to check the value of the $basket variable.
<?php
if ($basket!= "") {
Add items to the basket
}
?>
Fragment 2. Display baskets in a browser
<?php
if ($ses _basket_items>0) {
If there are items in the basket
For ($basket _counter=0 $basket _counter< $ses _basket_items $basket _counter++) {
Traverse the basket and print out each line
Of course you can format the display with a grid table.
Needs to be formatted to show the price score. If it is not formatted, then. 00 is not displayed
$price =sprintf ("%01.2f", $ses _basket_price[$basket _counter]);
$amount = $ses _basket_amount[$basket _counter];
$name = $ses _basket_name[$basket _counter];
echo "$amount $name $price";
echo "<br>\n";
}
} else {
There is no commodity in the basket.
Set item count to 0 and clear all variables
This is a cleanup process. It prevents people from getting the old array
$ses _basket_items=0;
unset ($ses _basket_name);
unset ($ses _basket_amount);
unset ($ses _basket_price);
unset ($ses _basket_id);
}
?>
This code will not produce any results. The item has not been filled, the basket is always empty, so the basket will not be displayed. So let's give
Add some items to the basket.
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.