Create a complete shoppingcart system with a couple lines of code
Create a complete shoppingcart system with a few lines of code.
Argument | Description |
---|---|
First |
Dictionary array with structure: “form element name value“ => “desired variable name/key used later when retreiving value“ See examples bellow |
[ | |
username_key |
What your $_SESSION[‘thisvalue’] is for your username system, if you have one. |
save_dir |
Folder to save the users shoppingcart |
encrypt |
Encrypt shoppingcart file, if false: users shoppingcart file will be named as the users username |
] |
$_SESSION[‘cart’] = new Cart($nameToVariable, [Options for saving cart]);
Simply leave options empty if you do not want cart saved in a file!
<?php include('robustcart.php') ?>
<html>
...
<!-- Optional: Create an iframe and add target="votar" in the form
to prevent page from refreshing each time the user adds an item to their cart -->
<iframe name="votar" style="display:none;"></iframe>
...
<form method="POST" target="votar">
<p value="1034" name="articlenumber"> 1034 </p>
<h1 value="Nuke" name="label"> Nuke </h1>
<img value="img/uranB12_nuke.jpg" src="img/uranB12_nuke.jpg" name="image">
<p value="Can be used as a toy or deadly weapon" type="text" name="description"> Can be used as a toy or deadly weapon </p>
<button type="submit" name="add_to_cart"> Add To Cart </button>
</form>
$values = array(
"articlenumber" => "articlenmb",
"label" => "label",
"image" => "img",
"description" => "desc"
);
$_SESSION['cart'] = new Cart($values, ["username_key" => "username", "save_dir" => "users", "encrypt" => TRUE]);
$_SESSION['cart'] = new Cart($values, []);
$_SESSION['cart'] = new Cart($values, ["username_key" => "user", "save_dir" => "shoppingcarts", "encrypt" => FALSE);
Can be done with a POST request containing a element with name=rfc (Remove-from-cart) and value=cart_index of item to remove
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
// Echo value of the description element in example form
echo $values['desc'];
// Items can be removed only with post
echo '<form method="POST"> <button type="submit" name="rfc" value="'.$values['cart_index'].'"> Remove </button> </form>';
}
Outputs “Can be used as a toy or deadly weapon” with a remove button