Simple php shopping cart implementation code and example
Session_start ();
- $ Conn = mysql_connect ("localhost", "root", "admin ");
- Mysql_select_db ("songyu ");
- // Check the number of times the array element appears
- Function check_count ($ array, $ element)
- {
- $ Times = 0;
- For ($ I = 0; $ I {
- If ($ element = $ array [$ I])
- {
- $ Times ++;
- }
- }
- Return $ times;
- }
- If (isset ($ _ GET ["p_id"])
- {
- $ P_id = $ _ GET ["p_id"];
- }
$ Total_price = 0;
- Array_push ($ _ SESSION ["cart"], $ p_id );
-
- $ Cart = $ _ SESSION ["cart"];
- Echo "your cart:
";
- $ New_array = array_count_values ($ cart );
- Foreach ($ new_array as $ key => $ value)
- {
- $ SQL = "select * from product where id = '". $ key ."'";
- $ Result = mysql_query ($ SQL );
- $ Out = mysql_fetch_array ($ result );
- Echo $ out [name]. "--- number:". $ value. "--". ($ out [price] * $ value )."
";
- $ Total_price = $ total_price + ($ out [price] * $ value );
- }
- Echo"
";
- Echo "----------- Total price --------------
";
- Echo $ total_price;
- ?>
- Back to s
2. login. php logon page
- If (isset ($ _ SESSION ["user"])
- {
- Unset ($ _ SESSION ["user"]);
- }
- If (isset ($ _ SESSION ["cart"])
- {
- Unset ($ _ SESSION ["cart"]);
- }
- ?>
3. product. php product page
- Session_start ();
- $ Conn = mysql_connect ("localhost", "root", "admin ");
- Mysql_select_db ("songyu ");
- $ SQL _product = "select * from product ";
- $ Res = mysql_query ($ SQL _product );
- If (! Isset ($ _ SESSION ["cart"])
- {
- $ _ SESSION ["cart"] = array ();
- }
- While ($ out2 = mysql_fetch_array ($ res ))
- {
- Echo "". $ out2 [name]."
";
- Echo $ out2 [price]."
";
- Echo "";
- }
- ?>
4. product_index.php product index page
- $ Conn = mysql_connect ("localhost", "root", "admin ");
- Mysql_select_db ("songyu ");
- $ SQL = "select * from user where username = '". $ _ POST ["username"]. "'and password = '". $ _ POST ["password"]. "'";
- $ Result = mysql_query ($ SQL );
- $ Out = mysql_fetch_array ($ result); // bbs.it-home.org
- If (! $ Out)
- {
- Echo "wrong! ";
- }
- Else
- {
- $ _ SESSION ["user"] = $ out [id];
- Echo "script window. location. href = 'product. php' script";
- }
- ?>
|