duanqie8549 2014-02-11 18:57
浏览 25

根据每个producto的键值添加每个阵列的额外成本

everyone, I have the following problem I need to solve... I'm building a shopping cart, everything was working fine until I decide to add some extras, so when you buy a product it let you add some extra items for an extra cost, so let say
Shoes $20.00 if you want to add an extra "coluor" it will cost you $10 extra...
so if you have only 1 item(one pair of shoes) it works fine, the problem is when you add an extra pair of shoes for a total of 2 items in your cart
Sport Shoes $20 + 10 per colour
Formal Shoes $30
so with that we can see that the total to be paid is only $60.00, but it shows $70.00 so why is that? what I have seen is that the next product get the extra cost from the first item, and if the next item has an extra, it get added plus the extras from the first extras of the first product... as you can see is kind of confusing.. so here is my code.


First, you need to understand how my array is build so here it is:

Normal Array for a single item:

Array
(
    [cart] => Array
        (
            [2] => Array
                (
                    [10] => Array
                        (
                            [quantity] => 1
                            [extra1] => 
                            [extra2] => 
                        )

                )

        )

)

So my session is that, the first ID[2] is the products ID, the second[10] is the Stock ID, the quantity and the extras... that works just fine... the problem is with the second.

Array
(
    [cart] => Array
        (
            [2] => Array
                (
                    [10] => Array
                        (
                            [quantity] => 1
                            [extra1] => Green +$10
                            [extra2] => Red +$10
                        )

                )

            [1] => Array
                (
                    [7] => Array
                        (
                            [quantity] => 1
                            [extra1] => 0
                            [extra2] => 0
                        )

                )

        )

)

so that's what the session looks like, I'm able to manipulate that array in any way I need, such as, update quantity, add extras, unset product or stock and so on... also the prices with discounts are display and sum or subtracted correctly the problem are the extras... which I don't understand why I have such a hard time to manipulate...

the code that I'm using to display the information is as follow:

$s_pq = $_SESSION['cart'];

$ext1 = 0;
$ext2 = 0;
foreach ($s_pq as $kes => $values){

    $qidq = $_SESSION['cart'][$kes];
    foreach ($qidq as $sks => $svs ){
        $ex_ex1 = $svs['extra1'];
        $ex_ex2 = $svs['extra2'];

        if ($ex_ex1 != '' && $ex_ex1 > 0  ) {
            $extt1 += 10;
        } else {$extt1 = 0;}
        if ($ex_ex2 != '' && $ex_ex2 > 0  ) {
           $extt2 += 10;
        } else {$extt2 = 0;}
    }
}

That little code is the same structure that I use to display the rest of the information:

$s_pq = $_SESSION['cart'];

foreach ($s_pq as $kes => $values){
// display product name and extra data related to that product ID
// DB Query to extract the info a few if's and echos..

    $qidq = $_SESSION['cart'][$kes];
    foreach ($qidq as $sks => $svs ){
        // Another few DB queries to fetch information for the stock for each item
    }
}

is a very simple code to work with the array from my session and is working fine, but the problem remains... so in the first code the idea is to see if the key [extra1] or [extra2] are different from " " if so it means that it has information other than " " which can be a single character but not empty and not space or zero's, if that is the case then add extra $10 if not then 0...

in another statement I use those variables $extt2 and $extt1 to add the values to the final cost

$final = round($db_price, 2) * $num_items_total  + $extt1 + $extt2;
echo '$ '.$final;

with that code I display the total cost for each item in the cart.
so that is all I have for the cart session it will have nothing else but that...
last but not least here is an array for multiple size with some extras...

Array
(
    [cart] => Array
        (
            [2] => Array
                (
                    [10] => Array
                        (
                            [quantity] => 1
                            [extra1] => asdasd
                            [extra2] => asdasd
                        )

                    [12] => Array
                        (
                            [quantity] => 1
                            [extra1] => 
                            [extra2] => 
                        )

                )

            [1] => Array
                (
                    [7] => Array
                        (
                            [quantity] => 1
                            [extra1] => as
                            [extra2] => asda
                        )

                )

        )

)

This is telling you that:

Name       | Descr              | Total
           | Size 10            |
Item [2]   |   quantity 1       | 
$20        |   Extra 1 +10      | $60.00   <-- Good total
           |   Extra 2 +10      |
           |--------------------|
           | Size 12            |
           |   quantity 1       |
-----------------------------------------
Item [1]   |   quantity 1       | 
$20        |   Extra 1 +10      | $60.00    <-- Wrong total
           |   Extra 2 +10      |          it should be $40.00
-----------------------------------------
Total                             $120.00

Thank you for taking the time, I appreciate any help you can provide!


F I X E D

I went through all my files line by line, and what i have discover was shocking! how could be so stupid!!! what happen was that I have 4 loops [loop [loop vars [loop [loop]]]] and that was the problem with the extras, the first 2 loops already show the information I need, I didn't see them because the file is damm long and I forgot about them, and the second loops I though I was outside the first 2 loops but I wasn't! and that was the problem, so I had to remove the other 2 loops and work with the first 2 loops and is working flawlessly; !! omg, 2 day!!, two day with that!! Thank you all!

So in other words, you can not use loops inside loops for the same purpose, basically was I was creating was a flood inside a loop, and for that in my case my variables never got the change to reset and that was because, the first loop was pulling the information when it was done it jump to the next key, but the loop inside was never finish so my vars never reset and that was the problem...
Again, thank you all.
  • 写回答

3条回答 默认 最新

  • dtpf76658 2014-02-11 19:13
    关注

    Hmm its always the little things that kills code so I'll start there. I noticed that $extt1 and $extt2 are never reset in the foreach loop. This may be the source of the incrementing extra cost error. They are initialized before that loop but never reset between items.

    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)