Jump to content

Wanting to know if I am going the right way with my coding style?


Recommended Posts

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>

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>

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.