Jump to content

[SOLVED] array structure question


aebstract

Recommended Posts

Just want some input on your thoughts for an array structure. Right now, for a shopping cart, each item is added as it's own id to a session and then split up in to an array. So 4,4,4,5,3. Then after splitting that up a      foreach ($contents as $id=>$qty) {

will tell us that we have qty 3 of item 4, 1 of 5 and 1 of 3. Would it be better to somehow make this a deeper array, something like:

 

array('item1' => array('1', '1', '1'),

        'item2' => array('1', '1'));

 

Then I can display item 1 and know I have 3 of it and item2 I have 2 of it? If so how do I go about storing something like this in a variable and how do I go about grabbing those results?

Link to comment
Share on other sites

Well if I have 3 of the same item and want to update it to 10, or 1... I'm having trouble figuring out how to easily update my quantity because of the way it is listed.

 

edit: I see what you mean but I am pretty stupid with arrays, gonna fiddle around and see if I can figure it out.

Link to comment
Share on other sites

Well if I have 3 of the same item and want to update it to 10, or 1... I'm having trouble figuring out how to easily update my quantity because of the way it is listed.

 

If the current quantity is 3 and you want to change it to 10, then simply update the value to 10.

 

Current cart:

$cart = array(
    //ID => Qty
    '3' => 1,
    '4' => 3,
    '5' => 1
)

 

Then if you need to change the quantity for product with the ID of 3 you would simply state

$cart[3] = 10;

Link to comment
Share on other sites

My current array when printed looks like this:

 

array(7) {

  [0]=>

  string(1) "8"

  [1]=>

  string(1) "8"

  [2]=>

  string(6) "11;4.2"

  [3]=>

  string(1) "7"

  [4]=>

  string(6) "11;4.3"

  [5]=>

  string(6) "11;4.2"

  [6]=>

  string(6) "11;4.2"

}

 

Needs to be formatted something like

 

array (

'11;4.2' => 3,

'11;4.2' => 3,

'11;4.2' => 3,

'8' => 2,

'7' => 1

)

 

Though I'm not sure how to set it up to work like that.

Link to comment
Share on other sites

Well, you have to create it like that. I'm assuming you have code already developed that creates the array when someone adds an item to the cart and it always adds a new element to the array. You will need to modify that code so it increments any existing elements.

 

Simple example for a function to add a single qty to an item in the cart

$cart = array();

function addItem($itemID)
{
    global $cart;

    if(!isset($cart[$itemID]))
    {
        $cart[$itemID] = 1;
        return;
    }
    $cart[$itemID]++;
}

 

Again, that is a "simple" example. A real solution would probably allow for the value to be set to a fixed qty or to increment/decrement the quantity as needed. It all depends on your particular needs.

Link to comment
Share on other sites

Okay, I'm gonna work with that in the morning. Currently all I am doing to make the cart is add the id of the item on to the end of a string. So I have $_SESSION['cart'] = "4,4,4,4"; and just if I wanted to add item 3 it would just get added to the end: 4,4,4,4,3. This leaves me pretty much option-less to do anything else ;(

Link to comment
Share on other sites

Right. So instead use this structure:

 

$_SESSION['cart'][ItemID] = Quantity;

 

$_SESSION['cart'][4] = 4;
$_SESSION['cart'][3] = 1;

 

So something like,

$_SESSION['cart'][$specialid] = 1;

right? With that, how would I work with the id and qty for each item? Like how can I set em as a variable or something?

 

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.