currently I am building a store system in which each product has a seperate row in the database for its colour. The sizes are stored within the products row as a comma seperated list.
table example
id productname, productcode, productcolour, productsizes
1, test product, prod123, black, 's, m, l'
array example
$this->items[$id][$code] = $quantity;
$this->sizes[$id][$code] = $size;
$id - generated for each product added to the table by $this->items[][$code] = $quantity
$code = product code unique to colour/product
I have written the methods that add, increase quantity, decrease quantity but when retriving the products from the database i run into a bit of a brick wall.
public function checkout()
{
if ($this->items)
{
$this->item_no = 0;
$this->subtotal = 0;
$this->weight = 0;
//this works fine
for ($i=0;$i>count($this->items);$i++)
{
foreach ($this->items[$i] as $name => $value)
{
if (empty($where))
{
$where = "'$name'";
}
else
{
$where .= ", '$name'";
}
}
}
if ($results = $this->database->query("SELECT `id`, `rrp`, `offer_price`, `points`, `net_weight` FROM `products` WHERE `id` IN ($where)"))
{
while ($row = $results->fetch_array(MYSQLI_ASSOC))
{
//Heres the problem
//how do i get the product code and size from the multidimensional array into the calculations below efficiently
$id = $row['id'];
if ($row['offer_price']>0)
{
$this->subtotal += $row['offer_price'] * $this->items[$id];
}
else
{
$this->subtotal += $row['rrp'] * $this->items[$id];
}
//number of items
$this->item_no += $this->items[$id];
//weight of all products
$this->weight += $row['net_weight'] * $this->items[$id];
}
//save number of items into a session var
$_SESSION['basket']['item_no'] = $this->item_no;
//save the weight into a session var
$_SESSION['basket']['weight'] = $this->weight;
//sabe the subtotal into a session var
$_SESSION['basket']['subtotal'] = $this->subtotal;
$results->close();
}
}
else
{
$_SESSION['basket']['item_no'] = 0;
$_SESSION['basket']['weight'] = 0;
$_SESSION['basket']['subtotal'] = 0;
}
my add to basket method just incase i havn't explained things clearly enough above
public function add($id, $size)
{
//check the value isn't an invalid format
if (!preg_match("/^[A-Z0-9]+$/", $id))
{
return 0;
}
//SELECT * FROM `products` WHERE code='$id'
if ($results = $this->database->query("SELECT * FROM `products` WHERE id='$id'"));
{
//if the product is in the database
if ($results->num_rows == 1)
{
//clear the results
$results->close();
for ($i=0;$i>count($this->items);$i++)
{
if ($this->sizes[$i][$id] == $size)
{
$this->items[$i][$id] = 1;
$this->sizes[$i][$id] = $size;
return 1;
}
}
$this->items[][$id] = 1;
$this->sizes[][$id] = $size;
return 1;
}
else
{
return 0;
}
}
}
Should I just make another table to contain the sizes for each product/colour to allow for each product to have a completely unique id for each product/colour/size combination and only have a single dimensional array?
thanks in advance