...

View Full Version : Having problems with arrays



Flic
08-10-2007, 02:31 AM
Hi,

I am using an array to pass information into a shopping cart.

I want to pass 3 bits of info into the array but only the first one is stored.

Also when I've been sending a different item in its been overwriting the original one instead of having both.

My code is:

<?php

require_once('inc/mysql.class.php'); // Include MySQL class
require_once('inc/global.inc.php'); // Include database connection
require_once('inc/functions.inc.php'); // Include functions
session_start(); // Start the session
$action = $_GET[action]; // The action from the URL
$id = $_GET[id]; // The product id from the URL
$size = $_GET[size];
$quan = $_GET[quan];

switch ($action) {
case 'add':
if ($cart) {
$cart .= ','.$_GET[id];
$cart .= ','.$_GET[size];
$cart .= ','.$_GET[quan].';';
} else {
$cart = $_GET[id];
}
break;
case 'delete':
if ($cart) {
$items = explode(',',$cart);
$newcart = '';
foreach ($items as $item) {
if ($_GET['id'] != $item) {
if ($newcart != '') {
$newcart .= ','.$item;
} else {
$newcart = $item;
}
}
}
$cart = $newcart;
}
break;
case 'update':
if ($cart) {
$newcart = '';
foreach ($_POST as $key=>$value) {
if (stristr($key,'qty')) {
$id = str_replace('qty','',$key);
$items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
$newcart = '';
foreach ($items as $item) {
if ($id != $item) {
if ($newcart != '') {
$newcart .= ','.$item;
} else {
$newcart = $item;
}
}
}
for ($i=1;$i<=$value;$i++) {
if ($newcart != '') {
$newcart .= ','.$id;
} else {
$newcart = $id;
}
}
}
}
}
$cart = $newcart;
break;
}
$_SESSION['cart'] = $cart;

$output[] = '<div id="items">';
echo showCart();
$output[] = '</div>';
?>

At the moment I'm just using direct urls to test it.

I have a vague memory from a few years ago that in Java you can define what would be stored in an array and wonder if you can do that in PHP (I can't find anything that says you can) and whether it would help if you can.

Many many thanks, I've been trying to fix this for a few days now!

Flic

SteveBach
08-10-2007, 02:43 AM
This seems like an overly complex solution...

Why not make an entire array a session variable, and then use subarrays in that?


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

if ($action == 'add') {
$_SESSION['cart'][] = array('id' => $id, 'size' => $size, 'quantity' => $quantity);

While it can be a matter of opinion, I think that anytime you're using comma delimited strings to store data, you should look for something more structured to use.

Flic
08-12-2007, 02:52 AM
Hm, so I get the idea of this. But with the example I've seen with an array in an array explicitly gives the first array an entry, eg:


$_SESSION['cart'][] = array("item" => array('id' => $id, 'size' => $size, 'quantity' => $quan));

So I'm not sure how to use it straight from the session and making it loop. It would need to loop through the different entries to pick out the different items and am not sure how to do that with this!

Thanks

Inigoesdr
08-12-2007, 02:59 AM
Something like

foreach($_SESSION['cart'] as $item)
{
$id = $item['item']['id'];
$size = $item['item']['size'];
}
for example.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum