【发布时间】:2019-01-20 16:37:44
【问题描述】:
我正在使用 CodeIgniter 购物车。我正在获取购物车详细信息,例如
Array
(
[dc54c1ce61893fa725cf87c9e20b4c78] => Array
(
[id] => 1
[name] => wertoiuy
[qty] => 1
[price] => 200
[options] => Array
(
[duration => 0
)
[rowid] => dc54c1ce61893fa725cf87c9e20b4c78
)
[e5966e762beda1762e461f27f1dc3ef4] => Array
(
[id] => 2
[name] => qwertf
[qty] => 1
[price] => 100
[options] => Array
(
[duration] => 6m
)
[rowid] => e5966e762beda1762e461f27f1dc3ef4
)
)
请注意,我收到了 [duration] => 6m 或 [duration] => 12m。
我从下拉列表中获得的持续时间。
<select name="yearDropdown" class="form-control dropdownDuration" >
<option selected disabled >Select duration</option>
<option value="12m">1 Year</option>
<option value="6m">6 months</option>
</select>
现在我正在做的是,在选择下拉菜单后,我点击添加到购物车。产品详细信息已添加到购物车中,但当我刷新页面时,我的下拉菜单会被清除。我的意思是下拉菜单再次显示Select duration。它应该显示我在将产品添加到购物车时选择的内容。
我尝试了一些代码
<?php if (in_array($prim->name_id, array_column($this->cart->contents(), 'id'))){
foreach ($this->cart->contents() as $product) {
if ($product['options']['duration'] == '12m') {
$yearSelected12='selected="selected"';
}
if ($product['options']['duration'] == '6m') {
$yearSelected6='selected="selected"';
}
break;
}
}?>
<select name="yearDropdown" class="form-control dropdownDuration" >
<option selected disabled >Select duration</option>
<option value="12m" <?php echo $yearSelected12;?>>1 Year</option>
<option value="6m" <?php echo $yearSelected6;?>>6 months</option>
</select>
<?php }?>
但它不起作用。任何其他想法如何解决这个问题?
你能帮我解决这个问题吗?
在建议@Praveen kumar 回答之后。我试过了
<?php if (in_array($prim->name_id, array_column($this->cart->contents(), 'id'))){
foreach ($this->cart->contents() as $product) {
//break;
} }?>
<select name="memberDuration" class="form-control dropdownDuration">
<option selected disabled>Select duration</option>
<option value="12m" <?php if($product['options'][ 'duration']=="12m"){ echo 'selected="selected"';} ?> >1 Year</option>
<option value="6m" <?php if($product[ 'options'][ 'duration']=="6m" ){ echo 'selected="selected"';} ?>>6 months</option>
</select>
此代码正在刷新,但最初我收到错误“未定义变量:产品”。
【问题讨论】:
标签: php html codeigniter-3 shopping-cart