【问题标题】:its showing variable undefined它的显示变量未定义
【发布时间】:2020-05-15 10:42:37
【问题描述】:

这是我的控制器页面

class Cart extends CI_Controller{ 

公共函数索引() {

    $getALlCat  = $this->Product_model-> getAllCategory();
     $data = array();

     //Retrieve cart Data
     $data['cartItems']  = $this->cart->contents();


    //print_r($data);exit;
    //$this->load->view('cart/index' ,$data);
    ///$this->load->helper('url');
 / /$this->load->view('cart/header',['getAllCate'=>$getALlCat], $data);    //including header and 
footer   
    $this->load->view('cart/cartbody',['getAllCate'=>$getALlCat], $data);

    $this->load->view('common/footer');    //including header and footer
  }
}



**This is View page**



<tbody>
                        <?php if($this->cart->total_items() > 0){ foreach($cartItems as $item){    ?>
                        <tr>
                            <td><img src="<?php echo $item['product_image']; ?>" width="50"/></td>
                            <td><?php echo $item['product_name']; ?></td>
                            <td><?php echo '$'.$item["product_price"].' USD'; ?></td>
                            <td><input type="number" class="form-control text-center"></td>
                            <td><?php echo '$'.$item["subtotal"].' USD'; ?></td>
                            <td>Are you sue</td>
                     </tr>
                     <?php } }else{ ?>
                        <td colspan="6"><p>Your cart is empty.....</p></td>
                        <?php } ?>
                    </tbody>






**Error Showing like this**

A PHP Error was encountered
Severity: Notice

消息:未定义变量:cartItems

文件名:cart/cartbody.php

行号:217

回溯:

文件:C:\xampp\htdocs\shop\application\views\cart\cartbody.php 线路:217 函数:_error_handler

文件:C:\xampp\htdocs\shop\application\controllers\Cart.php 线路:33 功能:查看

文件:C:\xampp\htdocs\shop\index.php 线路:315 函数:require_once

【问题讨论】:

    标签: codeigniter


    【解决方案1】:

    您可以将多个值存储为array key$data,然后仅发送$data 以查看和使用这些键来检索值。我已经在下面更正了您的代码,它应该可以满足您的需要。我已经在必要时提到了 cmets。

     public function index() {
    
        $data = array();
    
        $data['getALlCate']  = $this->Product_model-> getAllCategory(); // store it in an array instead (I've changed it to getALlCate from getALlCat, so you don't have to change anything in your view)
    
        //Retrieve cart Data
        $data['cartItems']  = $this->cart->contents();
    
        //$this->load->view('cart/header', $data); // including header and footer   
        $this->load->view('cart/cartbody', $data);
    
        $this->load->view('common/footer');    //including header and footer
      }
    }
    // you can retrieve data in view by accessing $cartItems and $getALlCate variables.
    

    希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      尝试关注

      $this->load->view('cart/cartbody',['getAllCate'=>$getALlCat, 'cartItems'=> $this->cart->contents()]);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-06
        • 2019-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-06
        • 1970-01-01
        相关资源
        最近更新 更多