PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/myproj/function.inc.php

http://jcuecafe.googlecode.com/
PHP | 144 lines | 109 code | 27 blank | 8 comment | 18 complexity | d1df4bbe5f431f1b32700973eabaa862 MD5 | raw file
  1. <?php
  2. // *****************
  3. //
  4. //Programmer : Mr. Suhas Talekar
  5. //Student ID : 12526175
  6. //Contact email : suhaskrishna.talekar@my.jcu.edu.au
  7. //
  8. // *****************
  9. function writeShoppingCart() {
  10. $cart = $_SESSION['cart'];
  11. if (!$cart) {
  12. return '<p>You have no items in your shopping cart</p>';
  13. } else {
  14. // Parse the cart session variable
  15. $items = explode(',',$cart);
  16. $s = (count($items) > 1) ? 's':'';
  17. return '<p>You have <a href="showMyCart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>';
  18. }
  19. }
  20. function showCart() {
  21. $hostname='localhost';
  22. $username='root';
  23. $pwd='';
  24. $dbname='jcu_e-cafe';
  25. $connect=mysql_connect($hostname,$username,$pwd) or die (" Not able to connect-s);");
  26. mysql_select_db($dbname,$connect);
  27. $cart = $_SESSION['cart'];
  28. if ($cart) {
  29. $items = explode(',',$cart);
  30. $contents = array();
  31. foreach ($items as $item) {
  32. $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
  33. }
  34. $output[] = '<form action="makepayment.php">';
  35. $output[] = '<table border=1 align=center>';
  36. $output[]='<tr><td align=center> </td><td align=center><b> Item </b></td><td align=center><b> Price </b></td><td align=center><b>Qty.</b></td><td align=center><b>Sub-total</b></td></tr>';
  37. foreach ($contents as $id=>$qty) {
  38. $query = 'SELECT * FROM item WHERE Item_ID = '.$id;
  39. $results= mysql_query($query) or die(mysql_error());
  40. $row = mysql_fetch_array($results);
  41. extract($row);
  42. $output[] = '<tr>';
  43. $output[] = '<td align=center><a href="showMyCart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
  44. $output[] = '<td align=center>'.$Item_Name.'</td>';
  45. $output[] = '<td align=center>$'.$Item_Price.'</td>';
  46. $output[] = '<td align=center>'.$qty.'</td>';
  47. $output[] = '<td align=center>$'.($Item_Price * $qty).'</td>';
  48. $total += $Item_Price * $qty;
  49. $output[] = '</tr>';
  50. }
  51. $output[] = '</table>';
  52. $output[] = '<p align=center>Grand total: $'.$total.'</p>';
  53. $output[] = '<div align=center> <button type="submit" align=center>Confirm Payment</button><br/></div>';
  54. $output[] = '</form>';
  55. } else {
  56. $output[] = '<p>You shopping cart is empty.</p>';
  57. }
  58. return join('',$output);
  59. }
  60. function update() {
  61. if ($cart) {
  62. $newcart = '';
  63. foreach ($_POST as $key=>$value) {
  64. if (stristr($key,'qty')) {
  65. $id = str_replace('qty','',$key);
  66. $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
  67. $newcart = '';
  68. foreach ($items as $item) {
  69. if ($id != $item) {
  70. if ($newcart != '') {
  71. $newcart .= ','.$item;
  72. } else {
  73. $newcart = $item;
  74. }
  75. }
  76. }
  77. for ($i=1;$i<=$value;$i++) {
  78. if ($newcart != '') {
  79. $newcart .= ','.$id;
  80. } else {
  81. $newcart = $id;
  82. }
  83. }
  84. }
  85. }
  86. }
  87. $cart = $newcart;
  88. }
  89. function insertorders($billid) {
  90. echo $billid;
  91. $hostname='localhost';
  92. $username='root';
  93. $pwd='';
  94. $dbname='jcu_e-cafe';
  95. $connect=mysql_connect($hostname,$username,$password) or die (" Not able to connect-s);");
  96. mysql_select_db($dbname,$connect);
  97. $cart = $_SESSION['cart'];
  98. echo $cart;
  99. if ($cart) {
  100. $items = explode(',',$cart);
  101. $contents = array();
  102. foreach ($items as $item) {
  103. $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
  104. }
  105. foreach ($contents as $id=>$qty) {
  106. $query = 'SELECT Item_Price FROM item WHERE Item_ID = '.$id;
  107. $results= mysql_query($query) or die(mysql_error());
  108. $row = mysql_fetch_array($results);
  109. $price=$row['Item_Price'];
  110. $tprice=$price*$qty;
  111. $query = 'Insert into `order` (`Order_ID`,`Bill_ID`,`Item_ID`,`Order_Item_Quantity`,`Order_Cost`) values(null,'.$billid.','.$id. ',' .$qty. ',' .$tprice.')';
  112. $results= mysql_query($query) or die(mysql_error());
  113. }
  114. } else {
  115. }
  116. }
  117. ?>