Shoppingcart gives same product PHP

445 views Asked by At

Afternoon,

I am having some difficulties with my shoppingcart for a webshop. In my cartArr (array that has the id's of my products got from my database) there are the right id's of my products that i bought. I also have a shoppingcart on my productspage that has the counts the right amount of items in my cartArr (so far so good).

When i want to show my products he only shows the last product i've clicked * times i've bought something. Logical because he doesn't remember it in my session array.

I know what my problem is but i can't figure out how to fix it.

/*Shoppingcart.php*/
<?php

$id = '';

    if(isset($_POST['shoppingCart']['btnSubmit']))
        {
            $id = $_POST['shoppingCart']['id'];

            $productDAO = new ProductDAO();
            $result = $productDAO->getProductById($id);

                if( in_array($id,$_SESSION['cartArr']))
            {
               array_push($_SESSION['cartArr'], $id);

            echo '<pre>';
            print_r('Hallo');
            echo '</pre>';

            }
        }

$smarty -> assign('arrProducts',$result);
$smarty -> assign('id',$id);
$smarty -> assign('cartArr',$_SESSION['cartArr']);
$smarty -> assign('product',$result)

/*Shoppingcart.tpl*/
{foreach $cartArr as $item}
    <div class="winkelkarItem">
        <img src="{$arrProducts.image}" class="winkelkarImgPosition" 
            alt="chimay" title="chimay"/>

        <ul class="winkelkarBeschrijving">
            <li>{$arrProducts.naam}</li>
            <li>{$arrProducts.prijs}&euro;</li>
            <li><input class="winkelkarAmmount" type="text" value="2"/></li>
            <li>7.08&euro;</li>
        </ul>
    </div>
{/foreach}

/*SQL statement to get my products out of my database in a seperate file ProductDAO*/

public function getProductById($id)
    {
         try
        {
            $sql = 'SELECT *
                    FROM tblProduct
                    WHERE id = :id';
            $stmt = $this->dbh->prepare($sql);
            $stmt->bindValue(':id',$id);
            $stmt->execute();
            $result = $stmt->fetch();
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }

        return $result;
    }
?>

Thx in advance for all help.

Short: Arrproducts has to get into my session Trough a foreach i need to make my items @ shoppingcart.tpl The id of the products i've clicked is in cartArr.

Thx!

1

There are 1 answers

2
jbrtrnd On

In a first time,

if( in_array($id,$_SESSION['cartArr'])) {
    array_push($_SESSION['cartArr'], $id);
}

If $id is in $_SESSION['cartArr'], why do you push it another time ?



In a second time, your last inserted item in your "shoppingCart" is $result (array fetched) and your "shoppingCart" is in array of ids stored in session.
So, in your template Shoppingcart.tpl, when you loop on $cartArr, $item is the id of your "product", not a variable who contains data of your "product". Then, in your foreach loop, you put $arrProducts.attribute, but $arrProducts corresponds to your last instert item.

So you have to get the data of each id passed in the loop :
(I'm not sure of the smarty template syntax, but try to understand what I mean)

            {$productDAO = new ProductDAO();}
            {foreach $cartArr as $item_id}
                {$item = $productDAO->getProductById($item_id);}
                <div class="winkelkarItem">
                    <img src="{$item.image}" class="winkelkarImgPosition"  alt="chimay" title="chimay"/>

                    <ul class="winkelkarBeschrijving">
                        <li>{$item.naam}</li>
                        <li>{$item.prijs}&euro;</li>
                        <li><input class="winkelkarAmmount" type="text" value="2"/></li>
                        <li>7.08&euro;</li>
                    </ul>
                </div>

            {/foreach}


EDIT :
Another solution, construct another array whith data of each "product", and pass it in you tpl.

In "Shoppingcart.php" :

$productDAO = new ProductDAO();
$items_datas = array();
foreach($_SESSION['cartArr'] as $item_id) {
    $items_datas[] = $productDAO->getProductById($item_id);
}
$smarty->assign("items_datas",$items_datas);

In you "Shoppingcart.tpl" :

            {foreach $items_datas as $item}

                <div class="winkelkarItem">
                    <img src="{$item.image}" class="winkelkarImgPosition"  alt="chimay" title="chimay"/>

                    <ul class="winkelkarBeschrijving">
                        <li>{$item.naam}</li>
                        <li>{$item.prijs}&euro;</li>
                        <li><input class="winkelkarAmmount" type="text" value="2"/></li>
                        <li>7.08&euro;</li>
                    </ul>
                </div>

            {/foreach}