Grigory Volkov
Grigory Volkov

Reputation: 93

passing an array to a view in CodeIgniter

I have a model which looks like this

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;

}

It gives me this array

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

In my Controller I have try to load a view and pass my array to it

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

And in the view run a foreach on the array and print things

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

It gives me errors 'illegal offset name,price etc...' How can I fix this problem? I need to print the information of the product and show images of it

Upvotes: 1

Views: 54

Answers (1)

Danish Ali
Danish Ali

Reputation: 2352

Don't use foreach() and try like this

echo $products["price"];

Only use foreach() on $products['image']

Upvotes: 1

Related Questions