Just archiving this project from my Open Source Software class.
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
// Add an item to the cart
|
||||
function add_item($key, $quantity) {
|
||||
global $products;
|
||||
if ($quantity < 1) return;
|
||||
|
||||
// If item already exists in cart, update quantity
|
||||
if (isset($_SESSION['cart12'][$key])) {
|
||||
$quantity += $_SESSION['cart12'][$key]['qty'];
|
||||
update_item($key, $quantity);
|
||||
return;
|
||||
}
|
||||
|
||||
// Add item
|
||||
$cost = $products[$key]['cost'];
|
||||
$total = $cost * $quantity;
|
||||
$item = array(
|
||||
'name' => $products[$key]['name'],
|
||||
'cost' => $cost,
|
||||
'qty' => $quantity,
|
||||
'total' => $total
|
||||
);
|
||||
$_SESSION['cart12'][$key] = $item;
|
||||
}
|
||||
|
||||
// Update an item in the cart
|
||||
function update_item($key, $quantity) {
|
||||
global $products;
|
||||
$quantity = (int) $quantity;
|
||||
if (isset($_SESSION['cart12'][$key])) {
|
||||
if ($quantity <= 0) {
|
||||
unset($_SESSION['cart12'][$key]);
|
||||
} else {
|
||||
$_SESSION['cart12'][$key]['qty'] = $quantity;
|
||||
$total = $_SESSION['cart12'][$key]['cost'] *
|
||||
$_SESSION['cart12'][$key]['qty'];
|
||||
$_SESSION['cart12'][$key]['total'] = $total;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get cart subtotal
|
||||
function get_subtotal() {
|
||||
$subtotal = 0;
|
||||
foreach ($_SESSION['cart12'] as $item) {
|
||||
$subtotal += $item['total'];
|
||||
}
|
||||
$subtotal = number_format($subtotal, 2);
|
||||
return $subtotal;
|
||||
}
|
||||
?>
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Configuration file for: Database Connection
|
||||
* This is the place where your database login constants are saved
|
||||
*
|
||||
* For more info about constants please @see http://php.net/manual/en/function.define.php
|
||||
* If you want to know why we use "define" instead of "const" @see http://stackoverflow.com/q/2447791/1114320
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* database host, usually it's "127.0.0.1" or "localhost", some servers also need port info, like "127.0.0.1:8080"
|
||||
*/
|
||||
define("DB_HOST", "localhost");
|
||||
|
||||
/**
|
||||
* name of the database. please note: database and database table are not the same thing!
|
||||
*/
|
||||
define("DB_NAME", "login");
|
||||
|
||||
/**
|
||||
* user for your database. the user needs to have rights for SELECT, UPDATE, DELETE and INSERT.
|
||||
* by the way, it's bad style to use "root". In a real application you should create a database user
|
||||
* that fits your needs.
|
||||
*/
|
||||
define("DB_USER", "root");
|
||||
|
||||
/**
|
||||
* The password of the above user
|
||||
*/
|
||||
define("DB_PASS", "");
|
||||
Reference in New Issue
Block a user