Un script de panier virtuel, qu'en pensez-vous
Posté : 03 avr. 2017, 18:45
Salutation,
Je travaille sur un panier virtuel, il fonctionne très bien, je partage le code surtout pour connaître vos avis et éventuellement y apporter des améliorations.
j'attends vos commentaires
Je travaille sur un panier virtuel, il fonctionne très bien, je partage le code surtout pour connaître vos avis et éventuellement y apporter des améliorations.
j'attends vos commentaires
Code : Tout sélectionner
<?php
define('ROOT', '..');
include(ROOT . '/init.php');
define('PATH', INIT_ROOT.'/store/');
define('PAGE_PATH', PATH.'product.php');
echo
PATH.'<br/>'
.PAGE_PATH;
?>
<!DOCTYPE html>
<html>
<head>
<style>
.sample1{
background:lightgray;
}
</style>
</head>
<body>
<h1>ONLINE STORE PRODUCT</h1>
<?php
//we build the shelf (can be too a mysql table);
$in_shelf=array(
"product1" => array("id"=>"1", "name" => "product 1"),
"product2" => array("id"=>"2", "name" => "product 2"),
"product3" => array("id"=>"3", "name" => "product 3"),
"product167" => array("id"=>"167", "name" => "product 167"),
"product512" => array("id"=>"512", "name" => "product 512")
);
print_r($in_shelf);
?>
<hr/>
<?php
echo 'PRODUCT<br/>';
foreach($in_shelf as $key=>$value)//we make the shelf more readable for the user;
{
echo $value['name'].', ';
if(!empty($_SESSION['cart_item']))//we check if the user got a caddy
{
if(in_array($value['id'],$_SESSION['cart_item']))//if yes, we check if the user already put this item into its caddy !
{
echo '<a href="'. PAGE_PATH .'?in_caddy">IN CADDY</a><br/>';
}
else
{
echo '<a href="'. PAGE_PATH .'?add='.$value['id'].'">ADD</a><br/>';
}
}
else
{
echo '<a href="'. PAGE_PATH .'?add='.$value['id'].'">ADD</a><br/>';
}
}
?>
<br/>
<?php
if(isset($_GET['add']))
{
if(in_array($_GET['add'],$_SESSION['cart_item']))//warning, there is already this intem into the caddy;
{
echo 'already into the caddy<br/>';
$already_in=true;
}
else
{
echo 'can be added into the caddy<br/>';
array_push($_SESSION['cart_item'],$_GET['add']);//!IMPORTANT, we add the item into the caddy;
}
$in_caddy=$_SESSION['cart_item'];
/*SIMPLE SCRIPT CHECK*/
print_r($in_caddy);echo '<br/>';//We check into the array;
foreach($in_caddy as $key => $value)
{
echo 'item_id = '.$value.' - ';
echo '<a href="'. PAGE_PATH .'?remove='.$value.'">REMOVE</a><br/>';
}
/*SIMPLE SCRIPT CHECK*/
header("Location: " .PAGE_PATH . "");
}
elseif(isset($_GET['remove']))//user can remove selected item from its caddy !;
{
$unset_array = array_search($_GET['remove'],$_SESSION['cart_item']);//!IMPORTANT
unset($_SESSION['cart_item'][$unset_array]);//!IMPORTANT
header("Location: " .PAGE_PATH . "?in_caddy");
}
elseif(isset($_GET['remove_all']))//Or all its items from its caddy !;
{
unset($_SESSION["cart_item"]);//!IMPORTANT
header("Location: " .PAGE_PATH . "?in_caddy");
}
elseif(isset($_GET['in_caddy']))//User can see what's in its caddy
{
echo 'IN CADDY<br/>';
if(!empty($_SESSION['cart_item']))
{
$in_caddy=$_SESSION['cart_item'];
foreach($in_caddy as $key => $value)
{
echo 'item_id = '.$value.' - ';
echo '<a href="'. PAGE_PATH .'?remove='.$value.'">REMOVE</a><br/>';
}
echo '<a href="'. PAGE_PATH .'?remove_all">REMOVE ALL</a> | ';
}
else
{
echo "Nothing into the caddy<br/>";
}
echo '<a href="'. PAGE_PATH .'">CLOSE CADDY</a> | ';
}
else
{
if(!empty($_SESSION['cart_item']))
{
echo '<a href="'. PAGE_PATH .'?in_caddy">SEE CADDY</a> | ';
}
else
{
$_SESSION['cart_item']=array();
}
}
?>
<hr/>
<a href="<?php echo PATH.'index.php';?>">Index</a>
</body>
</html>