Jump to content

Shopping Cart


Recommended Posts

Hi,

 

im trying to learn php, i followed a tutorial on the internet to build a shopping cart that uses a cookie to remember cart contents. The problem i have is i can set a cookie but none of my other functions are working, looking fro advice on what maybe wrong, thanks

 

<?php

session_start();

 

if(isset($_GET['action']))

{

 

$func = $_GET['action'];

 

                }

 

mysql_connect("localhost", "root", "p104728");

mysql_select_db("p104728");

 

 

function GetCartId()

{

 

// This function will generate an encrypted string and

// will set it as a cookie using set_cookie. This will

// also be used as the cookieId field in the cart table

 

if(isset($_COOKIE["cartId"]))

{

return $_COOKIE["cartId"];

}

else

{

// There is no cookie set. We will set the cookie

// and return the value of the users session ID

setcookie("cartId", session_id(), time() + ((3600 * 24) * 30));

return session_id();

}

 

switch($func)

{

case "add":

{

AddItem($_GET['id'], $_GET['qty']);

ShowCart();

break;

}

case "update":

{

UpdateItem($_GET['id'], $_GET['qty']);

ShowCart();

break;

}

case "remove":

{

RemoveItem($_GET['id']);

ShowCart();

break;

}

default:

{

ShowCart();

}

}

 

function AddItem($itemId, $qty)

{

$result = mysql_query("SELECT COUNT(*) from cart WHERE cookieID ='".GetCartId()."' AND itemID = $itemId");

 

echo $result;

 

$row = mysql_fetch_row($result);

$numRows = $row[0];

 

if($numRows == 0)

{

// This item doesn't exist in the users cart,

// we will add it with an insert query

 

mysql_query("INSERT INTO cart(cookieID, itemID, quantity) VALUES ('".GetCartId()."', $itemId, $qty)");

}

else

{

// This item already exists in the users cart,

// we will update it instead

UpdateItem($itemId, $qty);

}

}

 

function UpdateItem($itemId, $qty)

{

mysql_query("update cart set quantity = $qty where cookieID = '" .GetCartId(). "' and itemID = $itemId");

}

 

function RemoveItem($itemId)

{

mysql_query("delete from cart where cookieID = '".GetCartId()."'and itemId = $itemId");

}

?>

 

        <?php

function ShowCart()

{

$result = mysql_query("select * from cart inner join products on cart.itemID = products.ID where cart.cookieID = '".GetCartId()."'");

 

while($row = mysql_fetch_array($result))

{

// Increment the total cost of all items

$totalCost += ($row['quantity'] * $row['price']);

?>

 

<tr>

<td width="15%" height="25">

<font face="verdana" size="1" color="black">

<select name="<?php echo $row['itemID']; ?>" onChange= "UpdateQty(this)">

<?php

for($i = 1; $i <= 20; $i++)

{

echo "<option ";

if($row['quantity'] == $i)

{

echo " SELECTED ";

}

echo ">" . $i . "</option>";

}

?>

</select>

</font>

</td>

<td width="55%" height="25">

<font face="verdana" size="1" color="black">

<?php echo $row['name']; ?>

</font>

</td>

<td width="20%" height="25">

<font face="verdana" size="1" color="black">

£<?php echo number_format($row['price'], 2, ".", ",");?>

</font>

</td>

<td width="10%" height="25">

<font face="verdana" size="1" color="black">

<a href="cart.php?action=remove&id=<?php echo $row['itemID']; ?>">Remove</a>

</font>

</td>

</tr>

<?php

}

?>

 

<tr>

<td width="100%" colspan="4">

</td>

</tr>

<tr>

<td width="70%" colspan="2">

<font face="verdana" size="1" color="black">

<a href="products.php"><< Keep Shopping</a>

</font>

</td>

<td width="30%" colspan="2">

<font face="verdana" size="2" color="black">

<b>Total: £<?php echo number_format($totalCost, 2, ".", ","); ?></b>

</font>

</td>

          <?php

}

 

}

}

?>

</tr>

</table>

<script language="javascript">

 

function UpdateQty(item)

{

itemID = item.name;

newQty = item.options[item.selectedIndex].text;

 

document.location.href = 'cart.php?action=update&id='+itemId+'&qty='+newQty;

}

</script>

Link to comment
https://forums.phpfreaks.com/topic/236425-shopping-cart/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.