Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shoppingcart提供相同的产品PHP_Php_Shopping Cart_Webshop - Fatal编程技术网

Shoppingcart提供相同的产品PHP

Shoppingcart提供相同的产品PHP,php,shopping-cart,webshop,Php,Shopping Cart,Webshop,下午, 我在网店的购物车上遇到了一些困难。在我的cartArr(数组中包含从数据库中获取的产品id)中,有我购买的产品的正确id。我的产品页面上也有一个购物车,它的数量与我的购物车中的商品数量相当(目前为止还不错) 当我想展示我的产品时,他只展示我点击的最后一个产品*我买东西的次数。合乎逻辑,因为他在我的会话数组中不记得 我知道我的问题是什么,但我不知道如何解决它 /*Shoppingcart.php*/ <?php $id = ''; if(isset($_POST['sho

下午,

我在网店的购物车上遇到了一些困难。在我的cartArr(数组中包含从数据库中获取的产品id)中,有我购买的产品的正确id。我的产品页面上也有一个购物车,它的数量与我的购物车中的商品数量相当(目前为止还不错)

当我想展示我的产品时,他只展示我点击的最后一个产品*我买东西的次数。合乎逻辑,因为他在我的会话数组中不记得

我知道我的问题是什么,但我不知道如何解决它

/*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;
    }
?>
/*Shoppingcart.php*/
Thx的所有帮助提前

简称: 我必须参加我的会议 通过foreach,我需要在@shoppingcart.tpl制作我的物品 我单击的产品的id位于cartArr中

谢谢

第一次

            {$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}
如果
$id
处于
$\u会话['cartArr']
,为什么要另推一次?



第二次,您在“shoppingCart”中最后插入的项目是
$result
(获取数组),您的“shoppingCart”位于会话中存储的ID数组中。
因此,在模板Shoppingcart.tpl中,当您在
$cartArr
上循环时,
$item
是“产品”的id,而不是包含“产品”数据的变量。然后,在您的
foreach
循环中,您将
$arrProducts.attribute
,但
$arrProducts
对应于您的最后一个insert项。

因此,您必须获取循环中传递的每个id的数据:
(我不确定smarty模板的语法,但请尝试理解我的意思)

在您的“Shoppingcart.tpl”中:

{foreach$items\u数据作为$item}
  • {$item.naam}
  • {$item.prijs}&欧元
  • 7.08欧元
{/foreach}
第一次:当然必须是if(!in_数组…)。就像那样,但我想看看我的$_会话['cartArr']中有什么内容,所以我删除了“!”并忘了放回去。顺便说一句,我理解你的意思,但我以前做过。我知道我需要什么进入我的tpl文件,但我就是没能做到(我不知道怎么做,而且上面的解决方案不起作用。)
            {$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}
$productDAO = new ProductDAO();
$items_datas = array();
foreach($_SESSION['cartArr'] as $item_id) {
    $items_datas[] = $productDAO->getProductById($item_id);
}
$smarty->assign("items_datas",$items_datas);
            {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}