Reputation: 179
here is a small peice off code that i wrote. But I am not getting the data in the view. It says undefined variable in the view
Controller
$data= array();
$data['']= json_decode(file_get_contents('http://localhost:8888/api/colleges'));
$this->load->view('colleges/index',$data);
View
foreach($data as $college) :
?>
<ul>
<li><label>ID:</label> <?php echo $college;?></li>
</ul>
<?php endforeach;?>
Upvotes: 1
Views: 570
Reputation: 148
I think you might need to use: array_push($data,json_decode(file_get_contents('http://localhost:8888/api/colleges'))
or you need to specify an index for $data e.g. $data[0]
Upvotes: 0
Reputation: 3606
You need to use:
Controller:
$data['colleges']= json_decode(file_get_contents('http://localhost:8888/api/colleges'));
View:
foreach($colleges as $college)
Upvotes: 1
Reputation: 17487
CodeIgniter translates the $data array into variables based on their key to be used in your view.
So, if (in your controller) you had:
$data['poop'] = "Poop is stinky."
Then, in your view, you won't be using $data, you'll be using
echo $poop;
// Poop is stinky.
Upvotes: 0