dongqiu3254 2015-09-22 12:34
浏览 52

codeigniter中的购物车库不起作用

i have some problem in my libraries cart when i try to insert my product into the cart. could you help me.

here is my code link for product into the cart.

    <form method="post" action="<?php echo base_url(); ?>cart/add"> 
        <span style="width: 70%;"><input type="submit" class="fa fa-cart-arrow-down btn btn-primary btn-sm btn-block" value="Add to Cart" <?php echo "$mati";?>/></span>
           <input type="hidden" name="id_books" value="<?php echo $row->id_books; ?>"/>
           <input type="hidden" name="id_category" value="<?php echo $row->id_category; ?>"/>
           <input type="hidden" name="title" value="<?php echo $row->title; ?>"/>
           <input type="hidden" name="images" value="<?php echo $row->images; ?>"/>
           <input type="hidden" name="price" value="<?php echo $total; ?>"/>
           <input type="hidden" name="qty" value="1"/>
    </form>

and this is cart controller

    class Cart extends CI_Controller {

function __construct()
{
    parent::__construct();
    $this->load->library(array('cart'));
    session_start();
}


function index()
{
    $data['menu'] = 'cart';
    $this->load->view('v_cart', $data);
}

function add()
{
    $data = array(
        'id_books'      => $this->input->post('id_books'),
        'id_category'      => $this->input->post('id_category'),
        'qty'     => $this->input->post('qty'),
        'price'   => $this->input->post('price'),
        'images'   => $this->input->post('images'),
        'title'    => $this->input->post('title'));
    $this->cart->insert($data);
    /*print_r($data);*/
    /*echo "<meta http-equiv='refresh' content='0; url=".base_url()."cart/'>";*/
    redirect('cart','refresh');
}

and this is cart view

    <?php if($cart = $this->cart->contents()):
                ?>
                <?php echo form_open('cart/update'); ?>
                    <table id="cart-table" class="table table-condensed">
                        <thead>
                            <tr>
                                <th>Action</th>

                                <th>Image</th>
                                <th>Product</th>
                                <th>Price</th>
                                <th>Quanity</th>
                                <th>Sub Total</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php $i = 1; ?>
                            <?php foreach($cart as $item): /* $title = str_replace(' ', '-', $item['title']); ?>
                            <?php $price = $row['price']; $jumlah_desimal = "0"; $pemisah_desimal =""; $pemisah_ribuan =".";?>
                            <?php  /*echo form_hidden('rowid[]', $item['rowid']);*/ 
                             echo form_hidden($i.'[rowid]', $item['rowid']); ?>
                            <tr>
                                <th class="product-remove">
                                    <a class="remove" title="Remove this product" href="<?php echo base_url().'cart/delete/'.$item['rowid'];?>">×</a>
                                </th>
                                <th>
                                    <div class="media">
                                        <div class="relative">
                                            <a href="shop-single.html" title="">
                                                <img src="<?php echo base_url().$item['images'];?>" alt=""/>
                                            </a>
                                        </div>
                                    </div><!-- end media -->
                                </th>
                                <th>
                                    <a href="<?php echo base_url();?>books/detail/<?php echo $title;?>/<?php echo $item['id_category'];?>/<?php echo $item['id_books'];?> "><?php echo $item['title'];?></a>
                                </th>
                                <td>Rp. <?php echo $this->cart->format_number($item['price']); /*echo rupiah($item['price']); */?></td>
                                <td>
                                    <?php echo form_input(array('name' => $i.'[qty]', 'value' => $item['qty'], 'maxlength' => '3', 'class' => 'j')); ?> pcs
                                </td>
                                <td>
                                    Rp. <?php echo $this->cart->format_number($item['subtotal']);/*echo rupiah($item['subtotal']);*/ ?>
                                </td>
                            </tr>
                            <?php $i++; ?>
                            <?php endforeach; ?>
                            <tr>
                                <td colspan="5" style="text-align: right!important;"><b>Total </b>(biaya buku)</td>
                                <td>
                                    Rp. <?php echo $this->cart->format_number($this->cart->total()); ?>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                    <?php 
                    echo form_close(); 
                else:
                echo 'Sorry, Your Cart is empty';
                    endif;
                    ?>

the result always show

    Sorry, Your Cart is empty

where is the bug? anybody can help me :) thanks :)

  • 写回答

1条回答 默认 最新

  • douqipi9704 2015-09-23 11:35
    关注

    Codeigniter cart has predefined format: https://ellislab.com/codeigniter/user-guide/libraries/cart.html

    $data = array(
                   'id'      => 'sku_123ABC',
                   'qty'     => 1,
                   'price'   => 39.95,
                   'name'    => 'T-Shirt',
                   'options' => array('Size' => 'L', 'Color' => 'Red')
                );
    
    $this->cart->insert($data);
    

    So you code will be,

    function add()
    {
        $data = array(
            'id'=>someID,
            'name'=>SomeName,
            'qty'     => $this->input->post('qty'),
            'price'   => $this->input->post('price'),
            'options' =>array('id_books'=> $this->input->post('id_books'),'id_category'=> $this->input->post('id_category'),'images'   => $this->input->post('images'),'title'    => $this->input->post('title'));
    
        $this->cart->insert($data);
        /*print_r($data);*/
        /*echo "<meta http-equiv='refresh' content='0; url=".base_url()."cart/'>";*/
        redirect('cart','refresh');
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。