Welcome Guest, Not a member yet? Register   Sign In
newbie: making variables available?
#11

[eluser]sophistry[/eluser]
how is your footer.php view being called? is it being loaded inside another view file? like this:
Code:
$this->load->view('footer')
#12

[eluser]Bill the Grue[/eluser]
exactly. I have a controller called welcome.

Here's the code:

Code:
<?php

class Welcome extends Controller {

    function Welcome()
    {
        parent::Controller();    
    }
    
    function index()
    {
        $this->load->helper('url');
        $this->load->view('header');
        // $this->load->view('menu');
        $this->load->view('welcome_message');
        $this->load->view('footer');
        
    }




}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */

hmmm....

I just noticed I don't have a closing ?> here.... wonder if that's part of the problem?
#13

[eluser]jedd[/eluser]
Hi Bill,

Earlier on you said:
Quote: What I’m trying to do is grab data from a variable (is_logged_in). I’ve programmed in php a while ago, and I remember setting variables as global - I just don’t know how to do that.

In general terms, we don't like global variables. Of course, we have ways to fake them - such as using session data.

Here's two threads that I found that touch on some of the problems you've described - and hopefully give you some ideas for solving them.

[url="http://ellislab.com/forums/viewthread/110608/"]Using a controller to display a view[/url]
[url="http://ellislab.com/forums/viewthread/108661/"]Help with beginner login question[/url]

Neither touch on dx_auth, but if you search for dx_auth you should find a plethora of relevant pages.


Quote:I think this might be the essence of my problem - I don't /have/ a controller for my footer - it's just a view file. No controller. Does every view file need a controller?

No, not as such, though in reality every view you have will need to be called by a controller, somewhere. It might be by your MY_Controller, if you're using that construct, otherwise a normal controller - but lacking any such call means that the view never gets .. well .. viewed.

Having said that, I wouldn't create a footer controller (or a header one). I'd create those view snippets in my MY_Controller (I can find a few good threads on this approach if you're interested) instead.

And don't fret - we all started clueless. Well, apart from all the Dereks.
#14

[eluser]jedd[/eluser]
Quote:I just noticed I don't have a closing ?> here.... wonder if that's part of the problem?

No. In fact, we tend to discourage the use of closing PHP tags. Evil nasty pointless things, they are.

Check the style guide for more assurance on this subject.
#15

[eluser]sophistry[/eluser]
[quote author="Bill the Grue" date="1244677291"]
So if I use this view code for my footer:
Code:
<hr>
<a href="/">Home</a> | <a href="/auth/">Login</a> |<a href="/auth/logout/">Logout</a>
<br />
<br />

&lt;?php echo $is_logged_in; ?&gt;

&lt;/body&gt;
&lt;/html&gt;
I get this error:
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: is_logged_in

Filename: views/footer.php

Line Number: 6
I think I'm not understanding how variables are inherited. Do I need a footer controller?[/quote]

ok, no, as jedd said, you don't need a footer controller. you can define a variable for the view using the $data parameter sent to the view load method. so, instead of just calling footer view, you call footer view and pass an associative array with the variable name as the key and the value as the variable value like so:

Code:
$data['is_logged_in'] = TRUE;
$this->load->view('footer', $data);
#16

[eluser]Bill the Grue[/eluser]
CHA-CHING!

Thank you so much for the hand holding guys. The mental block? I didn't grok the controller is in charge of passing data to views - I was stuck on the view.

This code works like a charm.

Code:
if ($this->dx_auth->is_logged_in())  
         {  
             $data['is_logged_in'] = TRUE;  
         }  
         else  
         {  
             $data['is_logged_in'] = FALSE;  
         }
    
    
    
    
     $this->load->helper('url');
         $this->load->view('header');

    $this->load->view('welcome_message');
    $this->load->view('footer', $data);
        
    }
#17

[eluser]jedd[/eluser]
I think I heard your cha-ching from over here!

And here's something extra, for free .. I'm picking up xwero's fondness for the ternary operator (and why do in 8 lines what you can do in 1?)

Instead of this code block:
Code:
if ($this->dx_auth->is_logged_in())  
         {  
             $data['is_logged_in'] = TRUE;  
         }  
         else  
         {  
             $data['is_logged_in'] = FALSE;  
         }

... try this :
Code:
$data['is_logged_in'] = ($this->dx_auth->is_logged_in())  ?  TRUE  :  FALSE ;
#18

[eluser]sophistry[/eluser]
and i always like to make xwero's code smaller (which is usually almost impossible). in this case you can just assign the array key directly:
Code:
$data['is_logged_in'] = $this->dx_auth->is_logged_in();




Theme © iAndrew 2016 - Forum software by © MyBB