【问题标题】:cannot found the sending data from controller to view. but it work. [Codeigniter]找不到从控制器发送数据以查看。但它工作。 [代码点火器]
【发布时间】:2016-10-24 07:30:19
【问题描述】:

对不起。我使用 Codeigniter 制作购物车,这里是问题的详细信息。

  • 我想将数据从控制器发送到其他视图页面。我想要的数据是用户单击“添加到购物车”按钮的产品数量(我想在 footer.php 视图上回显产品数量。)
  • 我从其他人那里复制源代码,所以我有一点我不理解他的代码。
  • 我不明白的一点是他如何将数据从控制器方法“add”发送到视图名称“shopping/cart”,因为我没有找到类似 $this->load->view('shopping/购物车', $data)
  • 如果我也想将相同的数据发送到 footer.php 视图。我该怎么办?

控制器

<?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');        
    }   

shopping/cart.php 视图中的一些代码

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

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); ?>

【问题讨论】:

  • 什么是你的名为 cart 的库,使用 " $this->load->library(array('cart' "" 加载

标签: php codeigniter


【解决方案1】:

他正在使用 codeigniter 图书馆推车。查看购物车库的文档 - http://www.codeigniter.com/user_guide/libraries/cart.html

在控制器中将产品添加到购物车库..

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

然后使用

从视图中获取添加的产品
$this->cart->contents();

您也可以使用相同的方法为您的页脚获取产品

【讨论】:

  • 你的意思是不需要 $this->load->view('shopping/cart', $data) ?嗯..听起来像会话
  • 是的,我试着写 $this->cart->contents();在页脚视图页面中。但它仅在我使用控制器方法“添加”时才有效,但如果我使用其他控制器方法它不起作用。 (所有控制器方法都有页脚视图)请帮帮我T_T
猜你喜欢
  • 2013-02-27
  • 1970-01-01
  • 2018-08-15
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 2014-01-05
  • 2019-03-18
  • 1970-01-01
相关资源
最近更新 更多