/myproj/function.inc.php
PHP | 144 lines | 109 code | 27 blank | 8 comment | 18 complexity | d1df4bbe5f431f1b32700973eabaa862 MD5 | raw file
- <?php
-
- // *****************
- //
- //Programmer : Mr. Suhas Talekar
- //Student ID : 12526175
- //Contact email : suhaskrishna.talekar@my.jcu.edu.au
- //
- // *****************
-
- function writeShoppingCart() {
- $cart = $_SESSION['cart'];
- if (!$cart) {
- return '<p>You have no items in your shopping cart</p>';
- } else {
- // Parse the cart session variable
- $items = explode(',',$cart);
- $s = (count($items) > 1) ? 's':'';
- return '<p>You have <a href="showMyCart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>';
- }
- }
- function showCart() {
-
- $hostname='localhost';
- $username='root';
- $pwd='';
- $dbname='jcu_e-cafe';
-
- $connect=mysql_connect($hostname,$username,$pwd) or die (" Not able to connect-s);");
- mysql_select_db($dbname,$connect);
-
-
- $cart = $_SESSION['cart'];
- if ($cart) {
- $items = explode(',',$cart);
- $contents = array();
- foreach ($items as $item) {
- $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
- }
- $output[] = '<form action="makepayment.php">';
- $output[] = '<table border=1 align=center>';
- $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>';
- foreach ($contents as $id=>$qty) {
- $query = 'SELECT * FROM item WHERE Item_ID = '.$id;
- $results= mysql_query($query) or die(mysql_error());
- $row = mysql_fetch_array($results);
-
- extract($row);
- $output[] = '<tr>';
- $output[] = '<td align=center><a href="showMyCart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
- $output[] = '<td align=center>'.$Item_Name.'</td>';
- $output[] = '<td align=center>$'.$Item_Price.'</td>';
- $output[] = '<td align=center>'.$qty.'</td>';
- $output[] = '<td align=center>$'.($Item_Price * $qty).'</td>';
- $total += $Item_Price * $qty;
- $output[] = '</tr>';
- }
- $output[] = '</table>';
- $output[] = '<p align=center>Grand total: $'.$total.'</p>';
-
- $output[] = '<div align=center> <button type="submit" align=center>Confirm Payment</button><br/></div>';
-
- $output[] = '</form>';
- } else {
- $output[] = '<p>You shopping cart is empty.</p>';
- }
- return join('',$output);
- }
- function update() {
-
- if ($cart) {
- $newcart = '';
- foreach ($_POST as $key=>$value) {
- if (stristr($key,'qty')) {
- $id = str_replace('qty','',$key);
- $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
- $newcart = '';
- foreach ($items as $item) {
- if ($id != $item) {
- if ($newcart != '') {
- $newcart .= ','.$item;
- } else {
- $newcart = $item;
- }
- }
- }
- for ($i=1;$i<=$value;$i++) {
- if ($newcart != '') {
- $newcart .= ','.$id;
- } else {
- $newcart = $id;
- }
- }
- }
- }
- }
- $cart = $newcart;
-
- }
- function insertorders($billid) {
- echo $billid;
- $hostname='localhost';
- $username='root';
- $pwd='';
- $dbname='jcu_e-cafe';
-
- $connect=mysql_connect($hostname,$username,$password) or die (" Not able to connect-s);");
- mysql_select_db($dbname,$connect);
-
-
- $cart = $_SESSION['cart'];
- echo $cart;
- if ($cart) {
- $items = explode(',',$cart);
- $contents = array();
- foreach ($items as $item) {
- $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
- }
-
- foreach ($contents as $id=>$qty) {
-
- $query = 'SELECT Item_Price FROM item WHERE Item_ID = '.$id;
- $results= mysql_query($query) or die(mysql_error());
- $row = mysql_fetch_array($results);
- $price=$row['Item_Price'];
- $tprice=$price*$qty;
-
- $query = 'Insert into `order` (`Order_ID`,`Bill_ID`,`Item_ID`,`Order_Item_Quantity`,`Order_Cost`) values(null,'.$billid.','.$id. ',' .$qty. ',' .$tprice.')';
- $results= mysql_query($query) or die(mysql_error());
-
-
- }
-
- } else {
-
- }
-
- }
- ?>