[英]after adding the product in the cart and the user refreshes the page then how can I display the data which is added in the cart
I am using the CodeIgniter shopping cart. 我正在使用CodeIgniter购物车。 I am displaying records like 我正在显示类似的记录
Row 1 第1行
AFGFD ANAND
trdsd@gmail.com
ActivityName | duration | price | Action
cricket | Select duration | 100 | Add to cart
Row 2 第2行
POIUY ANAND
mnbsd@gmail.com
ActivityName | duration | price | Action
cricket | Select duration | 100 | Add to cart
Badminton | Select duration | 200 | Add to cart
Note duration is a select dropdown which is below 注意持续时间是一个选择下拉列表,它在下面
<select name="memberDuration" class="form-control dropdownDuration">
<option selected disabled >Select duration</option>
<option value="12m">1 Year</option>
<option value="6m">6 months</option>
</select>
Below is the final code which is working for. 下面是最终的代码。 There is no issue with add to cart as well. 添加到购物车也没有问题。 The user can't add the same product more than one. 用户最多只能添加一个产品。
foreach ($SActivity as $sec_data) {
$counter = 0; // COUNTER FOR KNOWING POSITION IN ACTIVITIES LOOP $num_activities = count($sec_data); // TOTAL NUMBER OF ACTIVITIES FOR THIS USER
foreach ($sec_data as $row) { $counter++; // ADD +1 TO COUNTER FOR EACH
ACTIVITY // ONLY PRINT OUT THE NAME AND TABLE HEADER IF FIRST ROW if($counter == 1){ ?>
<h2>
<?php echo $row->first_name;?>
<?php echo $row->last_name;?>
</h2>
<p>
<?php echo $row->email;?>
</p>
<table>
<thead>
<tr>
<th>ActivityName</th>
<th>duration</th>
<th>price</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php } ?>
<tr>
<td>
<?php echo $row->ActivityName;?>
</td>
<td>
<select name="memberDuration" class="form-control dropdownDuration">
<option selected disabled>Select duration</option>
<option value="12m">1 Year</option>
<option value="6m">6 months</option>
</select>
</td>
<td>
<div class="calActivitylPrice"></div>
</td>
<td>
<div class="display_table">
<?php echo $secActivity_Addtocart_active;?>
</div>
</td>
</tr>
<?php
// IF THIS IS THE LAST ACTIVITY OF THIS USER IN THE LOOP: PRINT TABLE END TAG
if($counter == $num_activities){
echo '</tbody></table>';
}
}
}
example of the added product. 添加产品的示例。
POIUY ANAND
mnbsd@gmail.com
ActivityName | duration | price | Action
cricket | 12m | 100 | remove
Badminton | 6m | 200 | remove
There is no issue till now. 到现在为止没有问题。
Now what I notice that I added a product in the cart and I refresh the page then my select dropdown and the price is not displaying. 现在,我注意到我在购物车中添加了产品,刷新页面,然后选择下拉列表,并且价格未显示。
I mean I added a product with 1 year
duration and according to the dropdown, I am displaying the price using ajax. 我的意思是我添加了一个期限为1 year
的产品,并且根据下拉列表,我使用ajax显示价格。
After adding the product in the cart and refresh the page then I am getting like 在将产品添加到购物车并刷新页面后,我得到
POIUY ANAND
mnbsd@gmail.com
ActivityName | duration | price | Action
cricket | Select duration | | remove
Badminton | Select duration | | remove
So I am trying to find the solution that after adding the product in the cart and the user refreshes the page then how can I display the data. 因此,我试图找到一种解决方案,在将产品添加到购物车中之后,用户刷新页面,然后如何显示数据。
I found some solution and it's working. 我找到了解决方案,它正在工作。 but the issue is my select dropdown is display in the all the select option and price is not displaying in the in the correct activity. 但问题是我的选择下拉列表显示在所有选择选项中,而价格未显示在正确的活动中。
I am getting the output like. 我得到的输出像。 I choose 1 year from the first-row select dropdown and it displaying in the all the row 我从第一行选择下拉列表中选择1年,它显示在所有行中
Row 1
AFGFD ANAND
trdsd@gmail.com
ActivityName | duration | price | Action
cricket | 12m | 100 | remove
Row 2
POIUY ANAND
mnbsd@gmail.com
ActivityName | duration | price | Action
cricket | 12m | 200 | remove
Badminton | 12m | | remove
<tr>
<td>
<?php echo $row->ActivityName;?>
</td>
<td>
<?php
$product['options']['duration']=0;
if (in_array($activityNO, 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>
</td>
<td>
<div class="calActivitylPrice">
<?php if (in_array($activityNO, array_column($this->cart->contents(), 'id'))){
foreach ($this->cart->contents() as $product) {
if ($product['id'] == $activityNO) {
//print_r($product['price']);
$priceAct = $product['price'];
}
//
}
print_r($priceAct);
}
?>
</div>
</td>
<td>
<div class="display_table">
<?php echo $Addtocart_active;?>
</div>
</td>
</tr>
Value of $this->cart->contents() $ this-> cart-> contents()的值
Array
(
[dc54c1ce61893fa725cf87c9e20b4c78] => Array
(
[id] => 1
[name] => cricket
[qty] => 1
[price] => 100
[options] => Array
(
[duration => 12m
)
[rowid] => dc54c1ce61893fa725cf87c9e20b4c78
)
[e5966e762beda1762e461f27f1dc3ef4] => Array
(
[id] => 2
[name] => Badminton
[qty] => 1
[price] => 200
[options] => Array
(
[duration] => 6m
)
[rowid] => e5966e762beda1762e461f27f1dc3ef4
)
)
Update according to update 根据更新进行更新
In this block 在这个街区
foreach ($this->cart->contents() as $product) {
You check $activityNO in the added carts and it is possibly true, but in foreach loop, you didn't get the correct $activityNO value from cart content. 您在添加的购物车中检查了$ activityNO,这可能是正确的,但是在foreach循环中,您没有从购物车内容中获得正确的$ activityNO值。
And by the way, your solution is not good, you are using many foreach. 而且,您的解决方案不好,您正在使用许多foreach。
To optimize it, only loop one time and prepare your date and then render. 要对其进行优化,只需循环一次并准备好日期然后进行渲染即可。
<?php
$productsInCart = [];
// Loop and prepare data similar to below
foreach ($this->cart->contents() as $cart) {
$productsInCart[$cart['id']] = $cart;
}
?>
<?php
foreach ($sec_data as $row) {
$activityNo = $row->activityNo; // I am not sure about this
?>
<h2>
<?php echo $row->first_name;?>
<?php echo $row->last_name;?>
</h2>
<select name="memberDuration" class="form-control dropdownDuration">
<option <?php if (empty($productsInCart[$activityNo])) { echo 'selected="selected"';} ?> disabled >Select duration</option>
<option value="12m" <?php if($productsInCart[$activityNo]['options']['duration'] == "12m"){ echo 'selected="selected"';} ?> >1 Year</option>
<option value="6m" <?php if($productsInCart[$activityNo]['options']['duration'] == "6m"){ echo 'selected="selected"';} ?>>6 months</option>
</select>
<?php
} // END foreach
?>
And apply the same strategy for below price. 并以低于价格的价格应用相同的策略。
Note: Above code is only the idea, it is not working 100%. 注意:上面的代码仅是个主意,并非100%有效。
To do that, you need to user one of suggestion solutions below, depends on you requirement. 为此,您需要使用以下建议解决方案之一,具体取决于您的要求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.