user1212207
user1212207

Reputation: 179

Not loading a view in Codeigniter

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

Answers (3)

Giovanni Della Cagna
Giovanni Della Cagna

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

Question Mark
Question Mark

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

jeffjenx
jeffjenx

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

Related Questions