dsedug8644 2016-10-24 07:30
浏览 23

无法找到从控制器发送数据查看。 但它的工作原理。 [笨]

Excuse me. I use Codeigniter to make shopping cart and here is detail of problem.

  • I want to send the data from controller to other view page. the data that I want is quantity of product that user click "add to cart" button (I want to echo quantity of product on the footer.php view.)
  • I copy source code from other so I have some point that I don't understand his code.
  • the point that I don't understand is how he send data from controller method "add" to the view name"shopping/cart" because I did not found something like $this->load->view('shopping/cart', $data)
  • if I want to send the same data to footer.php view too. How should I do?

Controller

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Shopping extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        //load model
        $this->load->model('billing_model');
        $this->load->library(array('cart', 'form_validation'));
    }

    public function index()
    {   
        $this->load->view('header');
                //Get all data from database
        $data['products'] = $this->billing_model->get_all();
                //send all product data to "shopping_view", which fetch from database.      
        $this->load->view('shopping/shopping_view', $data);

        $this->load->view('footer');
    }


    public function add()
    {
              // Set array for send data.
        $insert_data = array(
            'id' => $this->input->post('id'),
            'name' => $this->input->post('name'),
            'price' => $this->input->post('price'),
            'qty' => 1
        );      

                 // This function add items into cart.
        $this->cart->insert($insert_data);

                // This will show insert data in cart.
        $this->load->view('header');
        $this->load->view('shopping/cart');
        $this->load->view('footer');
         }

    public function remove($rowid) 
    {
                    // Check rowid value.
        if ($rowid==="all"){
                       // Destroy data which store in  session.
            $this->cart->destroy();
        }else{
                    // Destroy selected rowid in session.
            $data = array(
                'rowid'   => $rowid,
                'qty'     => 0
            );
                     // Update cart data, after cancle.
            $this->cart->update($data);
        }

                 // This will show cancle data in cart.
        redirect('shopping/add');
    }

    public function update_cart()
    {

                // Recieve post values,calcute them and update
                $cart_info =  $_POST['cart'] ;
        foreach( $cart_info as $id => $cart)
        {   
                    $rowid = $cart['rowid'];
                    $price = $cart['price'];
                    $amount = $price * $cart['qty'];
                    $qty = $cart['qty'];

                        $data = array(
                'rowid'   => $rowid,
                                'price'   => $price,
                                'amount' =>  $amount,
                'qty'     => $qty
            );

            $this->cart->update($data);
        }
        redirect('shopping/add');        
    }   

some code in shopping/cart.php view

<div id="cart" >
            <div id = "heading">
                <h2 align="center">Products on Your Shopping Cart</h2>
            </div>

                <div id="text"> 
            <?php  $cart_check = $this->cart->contents();

            // If cart is empty, this will show below message.
             if(empty($cart_check)) {
             echo 'To add products to your shopping cart click on "Add to Cart" Button'; 
             }  ?> </div>

                <table id="table" border="0" cellpadding="5px" cellspacing="1px">
                  <?php
                  // All values of cart store in "$cart". 
                  if ($cart = $this->cart->contents()): ?>
                    <tr id= "main_heading">  <!--เขียนแบบนี้ก็ได้เหมือนecho ต่อจากifนั่นเอง-->
                        <td>Product ID</td>
                        <td>Name</td>
                        <td>Price</td>
                        <td>Qty</td>
                        <td>Amount</td>
                        <td>Cancel Product</td>
                    </tr>
                    <?php
                     // Create form and send all values in "shopping/update_cart" function.
                    echo form_open('shopping/update_cart');
                    $grand_total = 0;
                    $i = 1;

                    foreach ($cart as $item):
                        //   echo form_hidden('cart[' . $item['id'] . '][id]', $item['id']);
                        //  Will produce the following output.
                        // <input type="hidden" name="cart[1][id]" value="1" />

                        echo form_hidden('cart[' . $item['id'] . '][id]', $item['id']);
                        echo form_hidden('cart[' . $item['id'] . '][rowid]', $item['rowid']);
                        echo form_hidden('cart[' . $item['id'] . '][name]', $item['name']);
                        echo form_hidden('cart[' . $item['id'] . '][price]', $item['price']);
                        echo form_hidden('cart[' . $item['id'] . '][qty]', $item['qty']);
                        ?>
                        <tr>
                            <td>
                       <?php echo $i++; ?>
                            </td>
                            <td>
                      <?php echo $item['name']; ?>
                            </td>
                            <td>
                                $ <?php echo number_format($item['price'], 2); ?>
                            </td>
                            <td>
                            <!--
                             $data = array(
                              'maxlength' => '3',
                              'size' => '1',
                              'type' => 'number',
                              'style' => 'text-align:right'
                            ); -->
                            <?php echo form_input('cart[' . $item['id'] . '][qty]', $item['qty'], "maxlength='3' size='1' style='text-align: right'"); ?>
                            </td>

some code in shopping_view.php

<?php    foreach ($products as $product) { ?>
    <form action="shopping/add" method="post" target="hiddenFrame">
    <?php

     echo form_hidden('id', $product['product_id']);
     echo form_hidden('name', $product['name']);   
     echo form_hidden('price', $product['price']); 
     echo form_submit($btn); ?>
  • 写回答

1条回答 默认 最新

  • dpe77294 2016-10-24 07:36
    关注

    He is using a codeigniter library cart. See documentation of cart library - http://www.codeigniter.com/user_guide/libraries/cart.html

    Adding products to the cart library in controller..

    $this->cart->insert($data);
    

    Then fetch the added products from the view using

    $this->cart->contents();
    

    You can also use the same method to get products for your footer

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog