Hi guys, I have a shopping cart finished for my site but I do not know how to make checkout function that will link to my cart.php I will show u my codes
index2.php
<?php
session_start();
include_once'../config.php';
include_once'../_header.php';
if(isset($_GET['page']))//get info from a get variable inside url
{
$pages = array("products", "cart");//define array variable
if(in_array($_GET['page'], $pages))//if does not exist in array will go to defualt page
{
$page = $_GET['page'];
}
else
{
$page ="products";
}
}
else
{
$page ="products";
}
?>
<div id="container">
<div id="main"><?php require($page. ".php"); // concatinate page that will call the defualt page?></div>
<div id="sidebar"><h1>Cart</h1>
<?php
if(isset($_SESSION['cart'])){
$query = "SELECT * FROM product WHERE ProductID IN (";
$result = mysqli_query($cxn, $query);
foreach($_SESSION['cart'] as $id => $value) {
$query .= $id. ",";
}
$query = substr ($query, 0, -1).") ORDER BY id_products ASC";
$result = mysqli_query($cxn, $query);
if(!empty($result))
{
while($row = mysql_fetch_array($result))
{
?>
<p><?php echo $row['ProductName'];?> <?php echo" x " . $_SESSION['cart'][$row ['ProductID']]['quantity']; ?></p>
<?php
}
}
else
{
echo "<p> Your cart is empty.<br />Please add some products</p>";
}
}
echo "<a href='../showfood.php?page=cart'>Go to Cart</a>";
?>
</div>
<?php
include_once'../_footer.php';
?>
cart.php
// updating the cart
if(isset($_POST['submit'])){
foreach($_POST as $key => $value){//checks all post, gives the key to equal value
$key = explode("-", $key);//explode to ghet rid of the hypthon i.e. quanitity - 2
$key = end($key);//ends the function and create variable into array
$key = explode("submit", $key);
$key = end($key);
if($_POST['quantity-' .$key] == 0){
unset($_SESSION['cart'][$key]);
} else{
$_SESSION['cart'][$key]['quantity'] = $value;
}
}
error_reporting(0);
}
// adding a new item
if(isset($_GET['action']) && $_GET['action'] == "add")
{
$id = intval($_GET['id']);
if(isset($_SESSION['cart'][$id])){ // ifthe product exists in the cart, increment the quanitity
$_SESSION['cart'][$id]['quantity']++;
}
else
{
// otherwise make a new item in the cart
$query2 = "SELECT * FROM product WHERE ProductID='$id'";
$result2 = mysqli_query($cxn, $query2) or die(mysqli_error($cxn));
if(mysqli_num_rows($result2) != 0)
{
$row2 = mysqli_fetch_array($result2);
$_SESSION['cart'][$row2['ProductID']] = array("quantity" => 1, "ProductPrice" => $row2['ProductPrice']);
}
else
{
$message = "This product id is invalid";
}
}
}
?>
<h1> View cart </h1>
<a href="../showfood.php" title ="Go back to products page">Go back to products page</a>
<?php $query = "SELECT * FROM product WHERE ProductID IN (";
$result = mysqli_query($cxn, $query);
foreach($_SESSION['cart'] as $id => $value) {//after each id there is a comma, makes sure query does not fail.
$result .= $id . ",";
}
$query = substr($query, 0, -1). ") ORDER BY name ASC";//gets rid of the last comma
$result = mysqli_query($cxn, $query);
if(empty($result)){// if query fails will echo out message, if not will continue to form
echo "<br /><span class='i'>You need to add an item before you can view it here</span>";
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>?page=cart">
<fieldset>
<table>
<tr>
<th>Name</th>
<th>Quantity</th>
<th>Price per item</th>
<th>Total Cost</th>
</tr>
<?php
$query = "SELECT * FROM product WHERE ProductID IN(";
foreach($_SESSION['cart'] as $id => $value){
$query .= $id . ",";
}
$query = substr($query, 0, -1). ") ORDER BY ProductName ASC";
$result = mysqli_query($cxn, $query);
$total_price = 0;
if(!empty($result)){//! mean "it is not" if query is not empty it will print out the while loop
while($row = mysqli_fetch_array($result)){
// price check
$price = $row['ProductPrice'];
if($row['ProductPrice'] == "0.00" || $row['ProductPrice'] == NULL)
{
if($row['ProductStarterPrice'] != '0.00' && $row['ProductStarterPrice'] != NULL)
{
$price = $row['ProductStarterPrice'];
}
else if($row['ProductSidePrice'] != '0.00' && $row['ProductSidePrice'] != NULL)
{
$price = $row['ProductSidePrice'];
}
}
$subtotal = $_SESSION['cart'][$row['ProductID']]['quantity']*$price;
$total_price += $subtotal;
?>
<tr>
<td><?php echo $row['ProductName'];?></td>
<td><input type = "text" name ="quantity-<?php echo $row['ProductID'];?>" size="5"
value="<?php echo $_SESSION['cart'][$row['ProductID']]['quantity'];?>" /></td>
<td><?php echo "£" . $price ?></td>
<td><?php echo "£" . $_SESSION['cart'][$row['ProductID']]['quantity']*$price;?></td>
</tr>
<?php
}
}
?>
<tr>
<td></td>
<td></td>
<td>Total Price</td>
<td><?php echo "£" . $total_price;// will give us a total price when user has bought the items
?></td>
</tr>
</table>
<input type="submit" name="submit" value="Update Cart" /></fieldset>
</form>
<p> To remove an item, set quantity to 0 </p>
Here is my databse tables to trigger once they have ordered
table1: order
id_order
date
user_id(links to my table called user)
table2:orderrow
rowID
orderID
ProductID(links to my other table called product)
I need to have a record that user can dislay, a thank you message for ordering and possibly viewing track record whenever the user have signed in.
im not sure if i missed out anything but correct me if i do.
hope u guys can give me a example of how to do it or give me a head start i really appreciate the help despite it be very difficult to explain :).
Thanks
Imran
quantity