【问题标题】:Cart block not updating codeigniter购物车块未更新 codeigniter
【发布时间】:2017-04-25 02:27:16
【问题描述】:

我有这个控制器:

<?php
class Cart extends CI_Controller{
  public $paypal_data = '';
  public $tax;
  public $shipping;
  public $total = 0;
  public $grand_total;

  /*
  *Cart Index
  */
  public function index(){
    //Load view
    $data['main_content'] = 'cart';
    $this->load->view('layouts/main', $data);
  }

  /*
  *Add To Cart
  */
  public function add(){
    //Item data
    $data = array(
      'id'=> $this->input->post('item_number'),
      'qty'=> $this->input->post('qty'),
      'price'=> $this->input->post('price'),
      'name'=> $this->input->post('title'),
    );
    //print_r($data);die();

    //Insert Into cart-block
    $this->cart->insert($data);

    redirect('products');
  }
  }

我有使用这个控制器的视图文件:

<div class="cart-block">
  <form action="cart/update" method="post">
    <table cellpadding="6" cellspacing="1" style="width:100%" border="0">
      <tr>
        <th>QTY</th>
        <th>Item Description</th>
        <th style="text-align:right">Item Price</th>
      </tr>
      <?php $i = 1; ?>
      <?php foreach ($this->cart->contents() as $items) : ?>
        <input type="hidden" name="<?php echo $i. '[rowid]'; ?>" value="<?php echo $items['rowid']; ?>" />
        <tr>
          <td><input type="text" name="<?php echo $i.'[qty]'; ?>" value="<?php echo $items['qty']; ?>" maxlength="3" size= "5"</td>
          <td><?php echo $items['name']; ?></td>
          <td style="text-align:right"><?php echo $this->cart->format_number($items['price']); ?></td>
        </tr>
        <?php $i++; ?>
      <?php endforeach; ?>
      <tr>
        <td></td>
        <td class="right"><strong>Total</strong></td>
        <td class="right" style="text-align:right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td>
      </tr>
    </table>
    <br>
    <p><button class="btn btn-default" type="submit">Update Cart</button>
    <a class="btn btn-default" href="cart">Go To Cart</a></p>
  </form>
</div>
<div class="panel panel-default panel-list">
  <div class="panel-heading panel-jeading-dark">
    <h3 class="panel-title">
      Categories
    </h3>
  </div>

  <!--List group -->
  <ul class="list-group">
    <?php foreach(get_categories_h() as $category) : ?>
    <li class="list-group-item"><a href="<?php echo base_url(); ?>products/category/<?php echo $category->id; ?>"><?php echo $category->name; ?></a></li>
    <?php endforeach; ?>
  </ul>

很抱歉,我无法向您展示实际的网站,因为它不在线,但根据上面的 sn-ps 能否指出导致购物车不更新的视图文件或控制器中的错误?

我正在关注视频教程,但似乎我做错了什么。这是视频:https://youtu.be/ajt_DJMS5FM?t=13m45s

【问题讨论】:

  • 你的 Cart 控制器中没有 update 方法,你的意思是不添加吗?

标签: javascript php jquery codeigniter


【解决方案1】:

您的表单操作是购物车/更新您在控制器中的功能是否添加?

将表单操作更改为购物车/添加。或者写一个购物车/更新函数。

【讨论】:

  • 跳到这里youtu.be/ajt_DJMS5FM?t=925它解释了控制器功能。
  • 是的,我现在一遍又一遍地看了它,这就是为什么它没有意义。我还添加了购物车更新功能,但受到相同问题的影响。
  • 检查它是否在正确的控制器上运行,如果是,那么可能是模型的设置问题
  • @ChristianEsperar 你会如何设计它?
【解决方案2】:

首先将表单操作 action="cart/update" 更改为

action="<?php echo base_url();?>cart/add" and apply die(); and test it

【讨论】:

  • 我在哪里申请 die(); ?
  • 好的,所以我应用了 die();就在数组之后,这样对吗?
【解决方案3】:

在控制器中加载购物车库

$this->load->library('cart');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    • 2019-12-25
    • 1970-01-01
    • 2014-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多