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

被刻印的时光 ゝ 提交于 2019-12-20 05:58:25

问题


I am using the CodeIgniter shopping cart. I am displaying records like

Row 1

AFGFD ANAND
trdsd@gmail.com

ActivityName   | duration        | price   | Action
cricket        | Select duration | 100     | Add to cart

Row 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.

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

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()

 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
            )

    )

回答1:


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.

And by the way, your solution is not good, you are using many 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%.


To do that, you need to user one of suggestion solutions below, depends on you requirement.

  1. Using Session to store cart info
    • Store cart to user Session when user add to cart
    • Load cart from Session if you want to read it (From the page you want to show)
    • Using this way, cart data is only available on Session scope
  2. Using database to store cart info
    • Store cart to your Database when user add to cart
    • Load cart from Database if you want to read it (From the page you want to show)
    • More complicated solution, but is is perpetual cart and can share among different browser for one user.


来源:https://stackoverflow.com/questions/54280987/after-adding-the-product-in-the-cart-and-the-user-refreshes-the-page-then-how-ca

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!