php 購物車的簡單實現代碼與樣本

來源:互聯網
上載者:User
  1. session_start();

  2. $conn=mysql_connect("localhost","root","admin");
  3. mysql_select_db("songyu");
  4. //檢查數組元素出現次數
  5. function check_count($array,$element)
  6. {
  7. $times=0;
  8. for($i=0;$i {
  9. if($element==$array[$i])
  10. {
  11. $times++;
  12. }
  13. }
  14. return $times;
  15. }
  16. if(isset($_GET["p_id"]))
  17. {
  18. $p_id=$_GET["p_id"];
  19. }

  20. $total_price=0;

  21. array_push($_SESSION["cart"], $p_id);
  22. $cart=$_SESSION["cart"];
  23. echo "your cart:
    ";
  24. $new_array=array_count_values($cart);
  25. foreach ($new_array as $key => $value)
  26. {
  27. $sql="select * from product where id='".$key."'";
  28. $result=mysql_query($sql);
  29. $out=mysql_fetch_array($result);
  30. echo $out[name]."---個數:".$value."--".($out[price]*$value)."
    ";
  31. $total_price=$total_price+($out[price]*$value);
  32. }
  33. echo "

    ";
  34. echo "-----------總價--------------
    ";
  35. echo $total_price;
  36. ?>
  37. 回s

複製代碼

2、login.php 登入頁

  1. if(isset($_SESSION["user"]))
  2. {
  3. unset($_SESSION["user"]);
  4. }
  5. if(isset($_SESSION["cart"]))
  6. {
  7. unset($_SESSION["cart"]);
  8. }
  9. ?>
複製代碼

3、product.php 產品頁

  1. session_start();
  2. $conn=mysql_connect("localhost","root","admin");
  3. mysql_select_db("songyu");
  4. $sql_product="select * from product";
  5. $res=mysql_query($sql_product);
  6. if(!isset($_SESSION["cart"]))
  7. {
  8. $_SESSION["cart"]=array();
  9. }
  10. while($out2=mysql_fetch_array($res))
  11. {
  12. echo "".$out2[name]."
    ";
  13. echo $out2[price]."
    ";
  14. echo "";
  15. }
  16. ?>
複製代碼

4、product_index.php 產品索引頁

  1. $conn=mysql_connect("localhost","root","admin");
  2. mysql_select_db("songyu");
  3. $sql="select * from user where username='".$_POST["username"]."' and password='".$_POST["password"]."'";
  4. $result=mysql_query($sql);
  5. $out=mysql_fetch_array($result); //bbs.it-home.org
  6. if(!$out)
  7. {
  8. echo "wrong!";
  9. }
  10. else
  11. {
  12. $_SESSION["user"]=$out[id];
  13. echo "";
  14. }
  15. ?>
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.