Welcome Guest, Not a member yet? Register   Sign In
Code getting messed up with foreach
#1

[eluser]ahandy[/eluser]
I'm working with the cart library, and I want a page that shows the items from the MySQL table, shows the add to cart link if the if isn't already in the cart, and shows Added to cart if it already is.

I know the concept, however whenever I try doing it, it either works with just 1 item (meaning, if I add only an item to the cart, it works fine, however if I add more than an item, the code starts getting messed up.)

Code:
// Checking to see if there is anything in the cart
if($this -> cart -> contents())
            {
                                // Foreach to get arrays from the cart lib
                foreach($this -> cart -> contents() as $wut)
                {
                                        // Checking to see if item is already added
                    if(in_array($item["Name"], $wut))
                    {
                            echo "<td>Added to cart.</td></tr>";

                    }
                        
                    else
                    {
                        echo "<td>Add</td>";
                        echo "</tr>";
                    }
                }            
            }

Like I said, if it's only an item, it works.
However if it's two, it shows Added to cart or Add like 5 times for each item (depending on how many items are available).
What I think the problem is, is that everything is working fine, however I need a way to only do the foreach if the item matches the foreach or w/e. I dunno, I'm kind of lost.

Help?

(I feel that my question isn't clear enough, if it isn't please tell me).

UPDATE:

Screenies:
[Image: irfFb.png]
[Image: aWAc9.png]
#2

[eluser]sorenchr[/eluser]
Maybe you should do a var_dump() for $this -> cart -> contents() containing 1 item, and one containing multiple items. You can post the results at something like <a href="http://pastebin.com">http://pastebin.com</a>
#3

[eluser]ahandy[/eluser]
Currently I'm just working with two items.

With one item:

Code:
array(1) { ["c4ca4238a0b923820dcc509a6f75849b"]=> array(6) { ["rowid"]=> string(32) "c4ca4238a0b923820dcc509a6f75849b" ["id"]=> string(1) "1" ["qty"]=> string(1) "1" ["price"]=> string(5) "30.99" ["name"]=> string(7) "T-Shirt" ["subtotal"]=> float(30.99) } }

With two:

Code:
array(2) { ["c4ca4238a0b923820dcc509a6f75849b"]=> array(6) { ["rowid"]=> string(32) "c4ca4238a0b923820dcc509a6f75849b" ["id"]=> string(1) "1" ["qty"]=> string(1) "1" ["price"]=> string(5) "30.99" ["name"]=> string(7) "T-Shirt" ["subtotal"]=> float(30.99) } ["c81e728d9d4c2f636f067f89cc14862c"]=> array(6) { ["rowid"]=> string(32) "c81e728d9d4c2f636f067f89cc14862c" ["id"]=> string(1) "2" ["qty"]=> string(1) "1" ["price"]=> string(5) "10.44" ["name"]=> string(5) "Pants" ["subtotal"]=> float(10.44) } }


EDIT:
Updated thread with screenies.
#4

[eluser]sorenchr[/eluser]
I don't think there's anything wrong with your foreach statement, can you post the PHP code you are using to add items to your cart?




Theme © iAndrew 2016 - Forum software by © MyBB