Welcome Guest, Not a member yet? Register   Sign In
Variable scoping - there has to be an easier way
#1

[eluser]jbly[/eluser]
hey all,

n00b here.

i was wanting to make my sidebars dynamic to the pages that they are specific to. basically i wanted to separate my main view with the sidebar associated with it.

what i did was section off the sidebar into its own file in /views/sidebars/s_dashboard.php for instance.

i include the sidebar i want to load in the array i pass from controller to the view that is being loaded and have a default sidebar load if nothing is set.

so far so good.

the issue is that the session variable that i define in the main view is then inaccessible in the sidebar loaded into that view. so, for instance: the dashboard.php has this at the top of it:

Code:
<?php
//grab user session
$sesh = $this->session->all_userdata();
?>

but then i get an error of undefined variable when the sidebar tries to use it.

i dont' want to have to paste the above code into the top of each sidebar, that seems inefficient and inappropriate. so too a global variable doesn't seem to be an elegant solution.

i'm not sure what to do here. and actually there may be a better way to deal with the user's session than how i was going about it to begin with, so i'm all ears to learning what the best, most efficient and elegant solution would be in this case.

thanks for your patience and help-
#2

[eluser]ojcarga[/eluser]
Noob or no please paste your code involved in the issue please, I am almost drunk and can't read too much text, I need more code Wink

Cheers!
#3

[eluser]jbly[/eluser]
[quote author="ojcarga" date="1348867032"]Noob or no please paste your code involved in the issue please, I am almost drunk and can't read too much text, I need more code Wink

Cheers![/quote]

the main thing in question is the code above.

i have that at the top of dashboard.php and then within that file have this:
Code:
<div class="three columns sidebar">  
    &lt;!-- sidebar loaded here--&gt;
    &lt;?php
    if(isset($sidebar))
    {
     $this->load->view('sidebars/'.$sidebar.'.php');
    }
    else
    {
     $this->load->view('sidebars/s_default.php');
    }
    ?&gt;
        
   </div>&lt;!-- end sidebar --&gt;

in that s_dashboard.php file i make calls to $sesh which is apparently out of scope.

cheers, have another one on me!
#4

[eluser]ojcarga[/eluser]
Two more things,

1) post the code where you create "$sidebar" var (The controller method) and call the "dashboard.php" view...

2) and why are you doing
Code:
$sesh = $this->session->all_userdata();
I mean, What is this for? It seems you are not using it in the view.
#5

[eluser]jbly[/eluser]
Code:
//load DASHBOARD view
  $data = array(
   'content'=>'dashboard',
   'friend_feed'=>$friend_feed['chunk'],
   'tags_chunk'=>$friend_feed['tags'],
   'tags_self'=>$tags_self,
   'stats'=>$stats,
   'sidebar'=>'s_dashboard'
  );
    
  $this->load->view('includes/template', $data);

and the $sesh var is used to drop things like the user's name, profile pic, etc. in with ease
#6

[eluser]ojcarga[/eluser]
Ok, it looks good, but how are you passing $sesh to s_dashboard.php?? I don't see it in the $data array.

Cheers!
#7

[eluser]jbly[/eluser]
there's the problem/question.

i'm creating the $sesh var in dashboard.php and then loading the s_dashboard.php sidebar view and am wondering why a variable created in a root view doesn't pass straight to a secondary view. do i just have to create it manually again?
#8

[eluser]jbly[/eluser]
ooo i'm an idiot.

i just realized what i need to do. create variable in dashboard.php like i was and then just pass it in to the load->view() call within the dashboard.php for the sidebar.

doi!

thanks for helping me see this!




Theme © iAndrew 2016 - Forum software by © MyBB