Advertisement
Guest User

Untitled

a guest
Jan 19th, 2014
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. <?php
  2.  
  3. function get_product_name($pid){
  4. $result=mysqli_query($db, "select name from products where serial=$pid");
  5. $row=mysqli_fetch_array($result);
  6. return $row['name'];
  7. }
  8. function get_price($pid){
  9. $result=mysqli_query($db, "select price from products where serial=$pid");
  10. $row=mysqli_fetch_array($result);
  11. return $row['price'];
  12. }
  13. function remove_product($pid){
  14. $pid=intval($pid);
  15. $max=count($_SESSION['cart']);
  16. for($i=0;$i<$max;$i++){
  17. if($pid==$_SESSION['cart'][$i]['productid']){
  18. unset($_SESSION['cart'][$i]);
  19. break;
  20. }
  21. }
  22. $_SESSION['cart']=array_values($_SESSION['cart']);
  23. }
  24. function get_order_total(){
  25. $max=count($_SESSION['cart']);
  26. $sum=0;
  27. for($i=0;$i<$max;$i++){
  28. $pid=$_SESSION['cart'][$i]['productid'];
  29. $q=$_SESSION['cart'][$i]['qty'];
  30. $price=get_price($pid);
  31. $sum+=$price*$q;
  32. }
  33. return $sum;
  34. }
  35. function addtocart($pid,$q){
  36. if($pid<1 or $q<1) return;
  37.  
  38. if(is_array($_SESSION['cart'])){
  39. if(product_exists($pid)) return;
  40. $max=count($_SESSION['cart']);
  41. $_SESSION['cart'][$max]['productid']=$pid;
  42. $_SESSION['cart'][$max]['qty']=$q;
  43. }
  44. else{
  45. $_SESSION['cart']=array();
  46. $_SESSION['cart'][0]['productid']=$pid;
  47. $_SESSION['cart'][0]['qty']=$q;
  48. }
  49. }
  50. function product_exists($pid){
  51. $pid=intval($pid);
  52. $max=count($_SESSION['cart']);
  53. $flag=0;
  54. for($i=0;$i<$max;$i++){
  55. if($pid==$_SESSION['cart'][$i]['productid']){
  56. $flag=1;
  57. break;
  58. }
  59. }
  60. return $flag;
  61. }
  62.  
  63. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement