【问题标题】:passing an array to a view in CodeIgniter将数组传递给 CodeIgniter 中的视图
【发布时间】:2019-03-08 09:36:58
【问题描述】:

我有一个看起来像这样的模型

function find($where){

    $arr = $this->db->get_where($this->table,$where)->result_array();

    foreach ($arr as $value) {

        $prod_id = $value["id"];

        $value['image']=$this->db->get_where("photos",['prod_id'=>$prod_id])->result_array();

    }

    return $value;

}

它给了我这个数组

Array(
[id] => 70
[name] => rrwerwer
[count] => 234
[price] => 234.00
[description] => 
[user_id] => 20
[image] => Array
    (
        [0] => Array
            (
                [id] => 88
                [prod_id] => 70
                [link] => 1_HP8l7LMMt7Sh5UoO1T-yLQ37.png
            )))

在我的控制器中,我尝试加载一个视图并将我的数组传递给它

$data = $this->productmodel->find(["user_id =" => $this->session->user["id"]]);
$this->load->view('myproduct', ["products" => $data]);

并在视图中对数组运行 foreach 并打印内容

<?php foreach($products as $prod){ ?>

<div class="productDiv">

   <div class="productName">

      <span class="productNameSpan">
        <?=$prod["name"]?>
      </span>

   </div>

    <div class="productPrice">

      <label class="productPriceLabel">Գին: <?=$prod["price"]." դրամ"?></label>

    </div>

    <div class="productCount">

      <label class="productCountLabel">Քանակ: <?=$prod["count"]."X" ?></label>

    </div>

    <div class="productDescribtion">

      <label class="productDescribtipnLabel">Նկարագիր: <?=$prod["description"] ?></label>

    </div>

    <div class="seeMoreDiv">

      <button class="seeMoreButton btn btn-success">

        ՏԵՍՆԵԼ ԱՎԵԼԻՆ

      </button>

    </div>
</div>

它给了我错误'非法偏移名称,价格等......' 我该如何解决这个问题? 我需要打印产品信息并显示产品图片

【问题讨论】:

  • $prod id 不是数组,这就是为什么你得到非法偏移错误请回显 $prod;die;或 print_r($prod);死;检查它是否是一个数组
  • 我觉得它看起来像对象数组,试试var_dumpprint_r

标签: php codeigniter


【解决方案1】:

不要使用foreach(),这样试试

echo $products["price"];

仅在$products['image'] 上使用foreach()

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-05
  • 1970-01-01
相关资源
最近更新 更多