PHP Implementation of Shopping Cart program code

Source: Internet
Author: User

Example 1

Recently in writing ANE-commerce website, in the shopping cart when found in PHP does not have a map of this data structure, we can not simply through a hashmap to implement the shopping cart, so I think of the array, through the format of the value of the array to write.
In other words, we are iterating through the values of the array, by splitting the value to determine whether the data is duplicated, for example, I now browse the product ID is 4, add the number is 3, I placed in the array of 4-3, in the future if you encounter is the ID is 4, we just need to change the number of this position in the array, If the ID does not exist within the array, the data is added to the array. Delete Shopping Cart The product that ID is also a cyclic array, which uses unset () to release this value.
The specific implementation code:
—————————————————————————————————————————–

The code is as follows Copy Code
<?php
Session_Start ();
if ($_session[' carts ']== "") {
$carts =array ();
Array_push ($carts, "0-0″");
$_session[' carts ']= $carts;
}
$method =$_get[' method '];
if ($method = = "Add") {
$productId =$_get[' productId '];
$number =$_get[' number '];
$carts =$_session[' carts '];
$flag = "false";
for ($i =0; $i <sizeof ($carts); $i + +) {
$PN = $carts [$i];
$pns =split ("-", $PN);
if ($pns [0]== $productId) {
$carts [$i]= $productId.] -". $number;
$flag = "true";
$location = $i;
}
}
if ($flag = = "true") {
$carts [$location]= $productId.] -". $number;
}else{
Array_push ($carts, $productId. " -". $number);
}
$_session[' carts ']= $carts;
}
if ($method = = "Delete") {
$productId =$_get[' productId '];
$carts =$_session[' carts '];
for ($i =0; $i <sizeof ($carts); $i + +) {
$PN = $carts [$i];
$pns =split ("-", $PN);
if ($pns [0]== $productId) {
Unset ($carts [$i]);
}
}
$_session[' carts ']= $carts;
}
?>

———————————————————————————————————–

E-commerce courses above the time to do a simple program, is an online shopping, but still 90 more points Oh! ~ In fact, as long as this kind of similar procedures, are simple additions and deletions to check the system, nothing but the operation of the database, we have to do is very good to achieve this business logic, as well as the design of the relevant fields to control, such as the state of the commodity control, is on-line or offline, or recommended.
Now look at the code that connects the database! ~
Database configuration code, in fact this can be set in the XML configuration file:

The code is as follows Copy Code
Class dbconfig{
var $databaseAddress = "180.153.178.89";
var $dataBaseUser = "Pantingwen";
var $databasePassword = "753116";
var $database = "Sqlpantingwen";
/**
* @return The $databaseAddress
*/
Public Function getdatabaseaddress () {
return $this->databaseaddress;
}
/**
* @return The $dataBaseUser
*/
Public Function Getdatabaseuser () {
return $this->databaseuser;
}
/**
* @return The $databasePassword
*/
Public Function Getdatabasepassword () {
return $this->databasepassword;
}
/**
* @return The $database
*/
Public Function Getdatabase () {
return $this->database;
}
/**
* @param field_type $databaseAddress
*/
Public Function setdatabaseaddress ($databaseAddress) {
$this->databaseaddress = $databaseAddress;
}
/**
* @param field_type $dataBaseUser
*/
Public Function Setdatabaseuser ($dataBaseUser) {
$this->databaseuser = $dataBaseUser;
}
/**
* @param field_type $databasePassword
*/
Public Function Setdatabasepassword ($databasePassword) {
$this->databasepassword = $databasePassword;
}
/**
* @param field_type $database
*/
Public Function Setdatabase ($database) {
$this->database = $database;
}
}
?>
Database Connection code:
Include_once ' dbconfig.php ';
Define ("Conn", Getcon ());
function Getcon () {
$dbConfig =new dbconfig ();
$con =mysql_connect ($dbConfig->getdatabaseaddress (), $dbConfig->getdatabaseuser (), $dbConfig-> Getdatabasepassword ());
return $con;
}
/**
* Select a database
* Enter description here ...
*/
function _select_db () {
$dbConfig =new dbconfig ();
if (!mysql_select_db ($dbConfig->getdatabase ()) {
Exit (' cannot find the specified database ');
}
}
/**
* Set Character Sets
* Enter description here ...
*/
function _set_names () {
if (!mysql_query (' Set names UTF8 ')) {
Exit (' character set error ');
}
}
function _query ($_sql) {
_select_db ();
_set_names ();
if (! $result =mysql_query ($_sql,conn)) {
Echo Mysql_error ();
}
return $result;
}
/**
* Only to get a data set
* Enter description here ...
* @param unknown_type $_sql
*/
function _fetch_array ($_sql) {
Return Mysql_fetch_array (_query ($_sql), MYSQL_ASSOC);
}
/**
* Return to Data group
* Enter description here ...
* @param unknown_type $_sql
*/
function _fetch_array_list ($_result) {
return mysql_fetch_array ($_RESULT,MYSQL_ASSOC);
}
/**
* Number of records affected
* Enter description here ...
*/
function _affect_rows () {
return Mysql_affected_rows ();
}
/**
* Determine if there is data
* Enter description here ...
* @param unknown_type $_sql
* @param unknown_type $_info
*/
function _is_repeat ($_sql,$_info) {
if (_fetch_array ($_sql)) {
}
}
function _close () {
if (!mysql_close (_conn)) {
Exit (' close exception ');
}
}
?>

Screenshot of Effect:

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.