Jump to content

[SOLVED] Adding Quanity To Databaseless shopping cart


TheJoey

Recommended Posts

i currently working with this very simple

but im having trouble trying to implement qty amount into it.

 

Also another trouble im having it once i add a item i cant add another one.

 

Hope you guys can help.

Thanks in advance

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

session_start();

$_SESSION['shopping_cart'] = array();

// And then to add items...

$_SESSION['shopping_cart'][] = "Ball";

$_SESSION['shopping_cart'][] = "Shoe";

// And to echo them back out...

foreach($_SESSION['shopping_cart'] as $value);

?>
<html>
<body>
<table>
<tr>
<td><a href='<?PHP echo $_SERVER['REQUEST_URI']; ?>?action=add_to_cart&item=Ball'>Add Ball To Cart</a></td>
</br>
</tr>
<tr>
<td><a href='<?PHP echo $_SERVER['REQUEST_URI']; ?>?action=add_to_cart&item=Shoe'>Add Shoe To Cart</a></td>
</tr>
<tr>
<td><select name="qty"><option value="0">--</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option></select></td></tr>
</tr>
</body>
</html>
<?php
if($_GET['action'] == "add_to_cart") {
  $_SESSION['shopping_cart'][] = $_GET['item'];
}
?>
<?php
foreach($_SESSION['shopping_cart'] as $key => $value) {
echo '<br> </br>';
echo $value . "<a href='" . $_SERVER['REQUEST_URI'] . "&action=remove_item&itemkey=" . $key ."'>Remove</a>";
}

if($_GET['action'] == "remove_item") {
unset($_SESSION['shopping_cart'][$_GET['itemkey']]);
}
$_SESSION['shopping_cart'] = array();
?>

Link to comment
Share on other sites

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

session_start();

$_SESSION['shopping_cart'] = array();

// And then to add items...

 

just look at this for a second... everytime you load this page you are clearing $_SESSION[shopping_cart] by setting it equal to a blank array.

 

you need to replace

$_SESSION['shopping_cart'] = array();

with

$_SESSION['shopping_cart'] = (!isset($_SESSION['shopping_cart']))?array():$_SESSION['shopping_cart'];

Link to comment
Share on other sites

VARIABLE=(CONDITION)?IF TRUE:IF FALSE;

 

same as

 

if (!isset($_SESSION['shopping_cart'])) {
  $_SESSION['shopping_cart'] = array();
}

 

so if session variable is already set (you added something to the cart) then dont set it if not then set it as a array...

 

Link to comment
Share on other sites

i was bored...

 

 

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
session_start();

$_SESSION['shopping_cart']=(!isset($_SESSION['shopping_cart']))?array():$_SESSION['shopping_cart'];

switch($_REQUEST['action']){
    case'add_to_cart';
        $_SESSION['shopping_cart'][$_POST['item']]['qty']=$_POST['qty'];
    break;
    case 'remove_item':
        unset($_SESSION['shopping_cart'][$_REQUEST['item']]);
    break;
}

?>
<html>
<body>


<form method="POST">
<input type=hidden name="action" value="add_to_cart" />
<input type=hidden name="item" value="Ball" />
Ball:
<input type="text" size=2 name="qty" />
<input type="submit" value="Add To Cart" name="submit" />
</form>

<form method="POST">
<input type=hidden name="action" value="add_to_cart" />
<input type=hidden name="item" value="Shoe" />
Shoe:
<input type="text" size=2 name="qty" />
<input type="submit" value="Add To Cart" name="submit" />
</form>


<b>CART</b>
<br />
<?php
foreach($_SESSION['shopping_cart'] as $item => $item_array) {
?>
<form method="POST">
<input type=hidden name="action" value="add_to_cart" />
<input type=hidden name="item" value="<?=$item;?>" />
<?=$item;?>:
<input type="text" size=2 name="qty" value="<?=$item_array['qty'];?>" />
<input type="submit"  value="Update" name="submit" />
<input type="button" onclick="window.location='http://<?php echo $_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME']; ?>?action=remove_item&item=<?=$item;?>';" value="Remove" name="remove" />
</form>
<br />
<?php
}
?>

</body>
</html>

Link to comment
Share on other sites

oh short tags...

 

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
session_start();

$_SESSION['shopping_cart']=(!isset($_SESSION['shopping_cart']))?array():$_SESSION['shopping_cart'];

switch($_REQUEST['action']){
    case'add_to_cart';
        $_SESSION['shopping_cart'][$_POST['item']]['qty']=$_POST['qty'];
    break;
    case 'remove_item':
        unset($_SESSION['shopping_cart'][$_REQUEST['item']]);
    break;
}

?>
<html>
<body>


<form method="POST">
<input type=hidden name="action" value="add_to_cart" />
<input type=hidden name="item" value="Ball" />
Ball:
<input type="text" size=2 name="qty" />
<input type="submit" value="Add To Cart" name="submit" />
</form>

<form method="POST">
<input type=hidden name="action" value="add_to_cart" />
<input type=hidden name="item" value="Shoe" />
Shoe:
<input type="text" size=2 name="qty" />
<input type="submit" value="Add To Cart" name="submit" />
</form>


<b>CART</b>
<br />
<?php
foreach($_SESSION['shopping_cart'] as $item => $item_array) {
?>
<form method="POST">
<input type=hidden name="action" value="add_to_cart" />
<input type=hidden name="item" value="<?php echo $item;?>" />
<?=$item;?>:
<input type="text" size=2 name="qty" value="<?php echo $item_array['qty'];?>" />
<input type="submit"  value="Update" name="submit" />
<input type="button" onclick="window.location='http://<?php echo $_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME']; ?>?action=remove_item&item=<?php echo $item;?>';" value="Remove" name="remove" />
</form>
<br />
<?php
}
?>

</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.