Okay, so I'm doing an e-commerce website for my school project. Basic run down of the cycle is that the user logs in, views items, adds the items that he likes to his shopping cart, and checks out the items at the shopping cart.

Now, for the the add to items function, I have this:

mysql_connect($localhost,$privName,$privPass);
        @mysql_select_db($database) or die("Unable to select database");
        $result = mysql_query($sel_rfo7);

        while ($row = mysql_fetch_array($result))
        {
            $itemChe[] = $row['cardAmount'];
        }
        echo '<font color="#FFFFFF" face="Verdana, Geneva, sans-serif">';
        echo 'RF Online Points Card 1000';
        echo '<br>';
        echo 'Items remaining: ';
        echo count($itemChe);
        echo '</font>';

Basically, I have an array ($itemChe) that adds the results of a query (a product). The count of this array is then used as "Quantity" of the product, as in, how many items are left. I have no problems in that array.

Now, when the user decides the amount of items he wants, he goes to the next page, which, supposedly adds the items to another array.

    global $itemName;
    global $itemQuan;
    $itemName[] = "Flyff20";
    $itemQuan[] = $itemtoBuy;
    $_SESSION['nameItem'] = $itemName;
    $_SESSION['quanItem'] = $itemQuan;}

And here in lies the problem. The $itemName and $itemQuan start with no values (they are passed from page to page via session), since the user does not have anything in his shopping cart yet. However, whenever I echo out the arrays via for loop, the first element in the arrays is always "Array". When the user adds an item, it only works the first time (so now the items in the array would be "Array", "Item A"). Any other attempts would simply overwrite "Item A".

Can anyone please tell me what I'm doing wrong, or why does the array overwrite the last item?

Hi Zack, the bug will probably lie in a piece of code that you haven't pasted.

It appears that the first piece of code you pasted isn't related to the second in any way. It doesn't show how values end up in $_SESSION or $_SESSION.

Please attach a complete source code and I will have a look.

A suggestion: name your variables in a way that reflects the plurality or singularity of their content. For example, $itemName sounds like this variable holds only one item name. I wouldn't expect an array of item names. For this you should name the variable $itemNames. Little details like this (called naming convention) will help you avoid bugs like assigning an array to a variable that is meant to hold a string (which is probably the core of your current bug).

Thanks for the advice. I fixed it, but I have no idea how I did it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.