Welcome Guest, Not a member yet? Register   Sign In
Include files - using session
#1

[eluser]Piekarz[/eluser]
Hi, I am writing cause i got issue with including file in my views.

I got the following code in my view:
Code:
<section id="head">
                &lt;header id="main"&gt;&lt;/header><nav id="main">&lt;?php require_once('/application/views/menu.php'); ?&gt;</nav>
            </section>

It include file menu.php. I used it for no repeat the code in many of view's files.
The problem is that when i try to get some data from session in including file i can't use same variables like in my view's file.

My menu.php file:
Code:
echo $_SESSION['somedata'];
or
Code:
echo $this->session->sess_read('somedata');

How can I force in including files to use my CI variables like sessions?
#2

[eluser]qcsites[/eluser]
If menu.php is a view file just load the view.
From codeigniter manual
Code:
$this->load->view('name');
#3

[eluser]TWP Marketing[/eluser]
[quote author="Piekarz" date="1347981476"]Hi, I am writing cause i got issue with including file in my views.

I got the following code in my view:
Code:
<section id="head">
                &lt;header id="main"&gt;&lt;/header><nav id="main">&lt;?php require_once('/application/views/menu.php'); ?&gt;</nav>
            </section>

It include file menu.php. I used it for no repeat the code in many of view's files.
The problem is that when i try to get some data from session in including file i can't use same variables like in my view's file.

My menu.php file:
Code:
echo $_SESSION['somedata'];
or
Code:
echo $this->session->sess_read('somedata');

How can I force in including files to use my CI variables like sessions?[/quote]

It is a better idea to read your session in the controller and pass the values via the view loader. This avoids a problem if your views are cached for display at a later time.

controller:
Code:
...
$data = array(
'somedata' => $this->session->get_userdata('somedata'),
...
):
...
$data['menu'] = $this->load->view('menu',$data,TRUE); // save the menu view as an HTML string
...
$this->load->view('page',$data);  // output your final page (a template) to the browser

In the menu view:

Code:
...
echo $somedata; // this is the value of 'somedata' from your session
...

In your final view (I called it "page", but use the name of your view:

Code:
<section id="head">
  &lt;header id="main"&gt;
...
  &lt;/header&gt;
<nav id="main">
&lt;?php
echo $menu; // this is the full menu view created in the controller
?&gt;
</nav>
</section>
...
#4

[eluser]Piekarz[/eluser]
Thank you for reply but i found my own way to solve this issue. I just noticed that when i try to use
Code:
$this->session->sess_read('somevar');

in my including menu file it didn't gives me what I wanted. But when I change it:
Code:
$this->session->userdata('somevar');
It's worked. Maybe i didn't read about difference between this 2 methods and I try to use them interchangeably but they are't same.

EDIT:

Ah and few words about it what your said. I know that better way is to read session from my controller but i need session var in this including file. Is $data visible in including files? If yes then you right ;p
#5

[eluser]TWP Marketing[/eluser]
[quote author="Piekarz" date="1348062747"]...

EDIT:

Ah and few words about it what your said. I know that better way is to read session from my controller but i need session var in this including file. Is $data visible in including files? If yes then you right ;p[/quote]

If you look at the call to load the view, you can see the that $data array is passed as the second parameter. Whatever variables you place in that array are exploded and made available in the view.
Do take note of the third parameter, a boolean value which is used to output the view as HTML in a string or sent directly to the browser. It makes the use of views and templates much easier.
#6

[eluser]Piekarz[/eluser]
[quote author="TWP Marketing" date="1348065728"]Whatever variables you place in that array are exploded and made available in the view.[/quote] Yeah I know, I use it a lot of time but i didn't use it for session. I don't know why ;]




Theme © iAndrew 2016 - Forum software by © MyBB