Welcome Guest, Not a member yet? Register   Sign In
Following the Video tutorial - Controller doesn't pass the variables to the view
#1

[eluser]stormbytes[/eluser]
So I'm following the video tuts, pretty simple stuff I'd think -

Problem is, the view is not receiving the variables from the controller.

Here's my code:

First the Controller:


Code:
[<?php

class Origin extends Controller
{

  function __construct()
  {
    parent::Controller();
  }

  function index()
  {
    $test = 123;
    
    $this->load->view('view_origin', $test);
  }

}
/code]

And the View...

[code]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
&lt;html&gt;
    &lt;head&gt;
        &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
        &lt;title&gt;Origin-View&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
      <h1>Where are you from?</h1>
      <p>This class is called from the Origin controller</p>
      <p>I just passed the following list variables:</p>
      &lt;?=$test?&gt;
    &lt;/body&gt;
&lt;/html&gt;

I've been racking my brains for hours! Tried this every which way. Tried passing an array. Nothing works -

I get that undeclared variable error.

Help!
#2

[eluser]whobutsb[/eluser]
In your controller do something like this:

Code:
$data['test'] = 123;

$this->load->view('my_view', $data);

And then in your view do:
Code:
&lt;?= $test; ?&gt;
#3

[eluser]stormbytes[/eluser]
[quote author="whobutsb" date="1286003227"]Instead of
Code:
&lt;?= $test; ?&gt;
use
Code:
&lt;?= $number; ?&gt;
[/quote]


Sorry I edited the code -

(been grasping at straws....)

the above code is updated - I removed the array variable and just left it simple. Still doesn't work -

I'm using Mamp. Could that have something to do with it?
#4

[eluser]whobutsb[/eluser]
Yea sorry about that. Just realized you weren't passing your variables in a array in your controller to your view. I updated my post a second after I noticed my mistake.
#5

[eluser]stormbytes[/eluser]
[quote author="whobutsb" date="1286003510"]Yea sorry about that. Just realized you weren't passing your variables in a array in your controller to your view. I updated my post a second after I noticed my mistake.[/quote

Okay -

Response part 1: You sir, Rock - Smile

Part 2: So always pass an array and call it by it's keys (as vars) in the view?
#6

[eluser]whobutsb[/eluser]
[quote author="stormbytes" date="1286003731"]
Part 2: So always pass an array and call it by it's keys (as vars) in the view?[/quote]

Yup. I usually use $data as my default variable that I pass controller data in to the view.
Code:
$data['view'] = 'contact_details';
$data['title'] = 'Contact Details Page';

$this->load->view('template', $data);
#7

[eluser]stormbytes[/eluser]
[quote author="whobutsb" date="1286003998"][quote author="stormbytes" date="1286003731"]
Part 2: So always pass an array and call it by it's keys (as vars) in the view?[/quote]

Yup. I usually use $data as my default variable that I pass controller data in to the view.
Code:
$data['view'] = 'contact_details';
$data['title'] = 'Contact Details Page';

$this->load->view('template', $data);
[/quote]

So it's gotta be an associative array then...

Can't do:

Code:
$data = array(1,2,3,4,5);

- unless:

$data = array('foo' =>'1', 'bar' =>'2')

Would that work?
#8

[eluser]whobutsb[/eluser]
Oh yeah, sorry forgot to mention that, yes it has to be an associative array.
#9

[eluser]techgnome[/eluser]
Yes, it has to be an associative array. The key part of the array is used as the name of the variable on the view side.

So given:
$data = array('foo' =>'1', 'bar' =>'2');

If you then pass $data to the view when you load it, you will get $foo and $bar in the view (where $foo will = 1, and $bar = 2).

Seems a bit nutters at first... but when you start to pass around query results, configuration flags, authentication, and other stuff, it makes some sense.

-tg
#10

[eluser]stormbytes[/eluser]
[quote author="techgnome" date="1286006057"]Yes, it has to be an associative array. The key part of the array is used as the name of the variable on the view side.

So given:
$data = array('foo' =>'1', 'bar' =>'2');

If you then pass $data to the view when you load it, you will get $foo and $bar in the view (where $foo will = 1, and $bar = 2).

Seems a bit nutters at first... but when you start to pass around query results, configuration flags, authentication, and other stuff, it makes some sense.

-tg[/quote]

Okay I think I'm starting to get the hang of it Smile

Now for some OOP....

Thanks so much for taking the time to view/respond to this thread. One of the main reasons I decided to invest in CodeIgniter over other frameworks was the community, which so far has exceeded my expectations!




Theme © iAndrew 2016 - Forum software by © MyBB