Freedom-n-Democrazy Posted October 5, 2011 Share Posted October 5, 2011 I am making a simple shopping cart. To add a product, I use this code: <?php if (isset ($_GET['action']) && $_GET ['action'] == 'add' ) { $id = intval($_GET['id']); $_SESSION['cart'][] = $id; } $cart = $_SESSION['cart']; if (!$cart) { echo "No product in cart."; } elseif ($cart) { foreach ($_SESSION['cart'] as $id) { echo $id; } } ?> <A href="index.html?action=add&id=1">Add to cart</A> I am going to be selling t-shirts, I'm going to need a size along with the product ID, so now I need to add a 'size' value. I've tried simply doubling up on the code above with this code, but it isn't working. I was wondering if I am going the right way about it? <?php if (isset ($_GET['action']) && $_GET ['action'] == 'add , size') { $id = intval($_GET['id']); $_SESSION['cart'][] = $id; $size = intval($_GET['size']); $_SESSION['cart'][] = $size; } $cart = $_SESSION['cart']; if (!$cart) { echo "No product in cart."; } elseif ($cart) { foreach ($_SESSION['cart'] as $id) { echo $id; } foreach ($_SESSION['cart'] as $size) { echo $size; } } ?> <A href="index.html?action=add&id=1&size=sizel">Add to cart</A> Link to comment https://forums.phpfreaks.com/topic/248460-wanting-to-know-if-i-am-going-the-right-way-with-my-coding-style/ Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 5, 2011 Author Share Posted October 5, 2011 The problem lays here, I think. if (isset ($_GET['action']) && $_GET ['action'] == add) { $id = intval($_GET['id']); $_SESSION['cart'][] = $id; $size = intval($_GET['size']); $_SESSION['cart'][] = $size; <A href="index.html?action=add&id=1&size=sizel">Add to cart</A> Link to comment https://forums.phpfreaks.com/topic/248460-wanting-to-know-if-i-am-going-the-right-way-with-my-coding-style/#findComment-1275904 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 5, 2011 Author Share Posted October 5, 2011 The correct way is: if (isset ($_GET['action']) && $_GET ['action'] == add || size) { Solved. Link to comment https://forums.phpfreaks.com/topic/248460-wanting-to-know-if-i-am-going-the-right-way-with-my-coding-style/#findComment-1275906 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.