smarty:打印$ [object Object] - $ [object Object]而不是value

时间:2017-12-07 08:17:12

标签: php smarty

我在隐藏输入字段中给出一个值

<input type='hidden' name='amount_{$smarty.foreach.product.index+1}' id="amount" value='{$product[0].price}'>

上面的代码打印

<input type="hidden" name="amount_1" id="amount" value="$[Object] - $[object Object]">

但如果我单独打印{$product[0].price}此值(不在输入字段中),则会打印正确的值

更新

我在这里设置值,产品ID名称和数量到cookie,我认为问题出在else部分,因为问题仅出现在第一个项目中..我也从数据库中获取价格。

function addtocart($product_id, $quantity){
        $results = $this->db->rawQuery("select * from products where id=?", [$product_id]);
        $id = $results[0]['id'];
        $name = $results[0]['name'];
        if(isset($_COOKIE['products'])){
            $products = $_COOKIE['products'];
            $products = json_decode($products,true);
            foreach($products as &$product){
                if($product['id'] == $product_id){
                    $product['quantity'] += $quantity;
                    setcookie('products',json_encode($products),strtotime( '+30 days' ));
                    echo 'done';
                    return;
                }
            }
            $item = ['id' => $id,'name' => $name,'quantity' => $quantity];
            $products[] = $item;
            print_r($products);
            setcookie('products',json_encode($products),strtotime( '+30 days' ));
            echo 'done';
        }else{
            //echo 'else';die();
            $products = array();
            $item = ['id' => $id,'name' => $name,'quantity' => $quantity];
            $products[] = $item;
            //print_r($products);
            setcookie('products',json_encode($products),strtotime( '+30 days' ));
            echo 'done';

        }
    } 

并在结帐页面

if(!empty($_COOKIE['products'])){
    $products_in_cart = json_decode($_COOKIE['products'],true);

    foreach($products_in_cart as $product){
        $db->where('id',$product['id']);
        $item = $db->get('products');
        $item[0]['quantity'] = $product['quantity'];
        $products[] = $item;
    }
}
$objSmarty->assign('products',$products);

Array-我使用{$ products | print_r}:

将其打印在模板文件中
Array
   (
    [0] => Array
        (
            [0] => Array
                (
                    [id] => 4
                    [name] => Rug Rat
                    [code] => Part #E13
                    [price] => 59.95
                    [image] => acc2.jpg
                    [catid] => 3
                    [subcatid] => 0
                    [description] =>product One description.
                    [status] => Y
                    [quantity] => 2
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [id] => 6
                    [name] => Mophead Floor Tool 
                    [code] => Part #A52 
                    [price] => 36.95
                    [image] => acc4.jpg
                    [catid] => 3
                    [subcatid] => 0
                    [description] => product two description.
                    [status] => Y
                    [quantity] => 1
                )

        )

)

在模板文件中:

{foreach from=$products item='product' name='product'}
{$product[0].price|trim}
    <input type='hidden' class="name" name='item_name_{$smarty.foreach.product.index+1}' value='{$product[0].name|escape}'>
    <input type='hidden' name='amount_{$smarty.foreach.product.index+1}' id="amount" value='{$product[0].price}'>
    <input type="hidden" name="quantity_{$smarty.foreach.product.index+1}" value="{$product[0].quantity|escape}">
{/foreach}

0 个答案:

没有答案