Kalai M Kalai M - 20 days ago 4
PHP Question

how can i access an array in my controller file to view file in codeigniter

controller file

public function get_product(){
//$idval = $this->input->get();
$productID = $this->uri->segment(3);
$datas = array();
$this->load->model('product_modal');
$datas = $this->product_modal->get_productdetail($productID);
print_r($datas);
echo $datas[0]->name;
$this->load->view('product_detail', $datas);
}


view file

<?php
$i=1;
foreach($result as $key => $array){?>
<table>
<tr>
<td><label >Product name</label></td>
<td><input type="text" class="form-control" id="p_name" name="p_name" value="<?php echo $array[0]->name; ?>" > <br /></td>
</tr>
<tr>
<td><label >Product Desc</label></td>
<td><textarea type="text" class="form-control" id="p_desc" name="p_desc" value="<?php echo $array[0]->description;?>" > </textarea></td>
</tr>
<tr>
<td><label >Qty</label></td>
<td><input type="text" class="form-control" id="p_qty" name="p_qty" value="<?php echo $array[0]->qty;?>" > <br /></td>
</tr>
<tr>
<td><label >Rate</label></td>
<td><input type="text" class="form-control" id="p_rate" name="p_rate" value="<?php echo $array[0]->rate;?>" > <br /></td>
</tr>
<tr>
<td><label >Amount</label></td>
<td><input type="text" class="form-control" id="p_amt" name="p_amt" value="<?php echo $array[0]->amt;?>" > <br /></td>
</tr>
</table>
<?php }?>


my output array like this

Array ( [0] => stdClass Object ( [id] => 3 [name] => trans [description] => fgt [qty] => 560 [rate] => 12 [amt] => 6720 ) )


how can i access an array in my controller file to view file


A PHP Error was encountered

Severity: Notice

Message: Undefined variable: result

Filename: views/product_detail.php

Line Number: 26

Backtrace:

File:
F:\xampp\htdocs\Product-Store\application\views\product_detail.php
Line: 26 Function: _error_handler

File:
F:\xampp\htdocs\Product-Store\application\controllers\Product.php
Line: 69 Function: view

File: F:\xampp\htdocs\Product-Store\index.php Line: 315 Function:
require_once

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/product_detail.php

Line Number: 26

Backtrace:

File:
F:\xampp\htdocs\Product-Store\application\views\product_detail.php
Line: 26 Function: _error_handler

File:
F:\xampp\htdocs\Product-Store\application\controllers\Product.php
Line: 69 Function: view

File: F:\xampp\htdocs\Product-Store\index.php Line: 315 Function:
require_once

Answer

You are getting this Message: Undefined variable: result message because there is no index with result key.

Try controller:

 $this->load->view('product_detail', array("data"=> $datas));

in view

<?php
$i=1;
foreach($data as $key => $array){?>