Welcome Guest, Not a member yet? Register   Sign In
Session question?
#1

[eluser]Jamongkad[/eluser]
Hey guys! I know this may sound quite basic but I was wondering how can one use session variables to pass data from one page to another.

Like lets say I had multiple forms, the user would fill out data for page 1. Then he would jump to page 2 but still have the data store in a cookie from page 1, and so on and so forth. Until he reaches the final page where all of his data is summarized for him. And he sends it out as an email.

Are there any examples of that type of usage? I mean are there best practices with regards to how to use Code Igniter for such a task?
#2

[eluser]FuriKuri[/eluser]
Something like this:

//to save data:
$this->session->set_userdata('name','peter');

//to recover data:
echo $this->session->userdata('name'); // this will print 'peter'.
#3

[eluser]Jamongkad[/eluser]
[quote author="FuriKuri" date="1186611383"]Something like this:

//to save data:
$this->session->set_userdata('name','peter');

//to recover data:
echo $this->session->userdata('name'); // this will print 'peter'.[/quote]

Thanks! is this possible? I was thinking within this line. And where do I place this code? in the view file?
Code:
$this->session->set_userdata('name',$_POST['name']);
#4

[eluser]FuriKuri[/eluser]
[quote author="Jamongkad" date="1186615056"][quote author="FuriKuri" date="1186611383"]Something like this:

//to save data:
$this->session->set_userdata('name','peter');

//to recover data:
echo $this->session->userdata('name'); // this will print 'peter'.[/quote]

Thanks! is this possible? I was thinking within this line. And where do I place this code? in the view file?
Code:
$this->session->set_userdata('name',$_POST['name']);
[/quote]

Yeah this code works. Im using it in my project.

You can place it where u want, controller or view. For example if you want to write the name of the user use <?=$this->session->userdata('name');?> in the view file.

PS: Recheck my thread
#5

[eluser]Michael Wales[/eluser]
Because of the multiple pages that will be storing data - and not wanting to bloat the session that much - I would just store it all within one variable.

A comma-delimited list works nicely, then you can implode() and explode() till your hearts content.
#6

[eluser]Jamongkad[/eluser]
[quote author="walesmd" date="1186640265"]Because of the multiple pages that will be storing data - and not wanting to bloat the session that much - I would just store it all within one variable.

A comma-delimited list works nicely, then you can implode() and explode() till your hearts content.[/quote]

Umm how do I go about coding that in? Sorry I'm kinda new to Code Igniter and PHP in general....is this what you're talking about?
Code:
view:

<?php
$arr = array(
        'name' => 'magicmeal'
       );
$this->session->set_userdata($arr);

?>

&lt;input type="radio" name="magicmeal" value="magicmeal 1"&gt; option 1 :<br>

&lt;input type="radio" name="magicmeal" &gt; option 2 : <br>

&lt;input type="radio" name="magicmeal" &gt; option 3 : <br>

&lt;input type="radio" name="magicmeal" &gt; option 4 : <br>

&lt;input type="radio" name="magicmeal" &gt; option 5 :  <br>

Code:
view2:
//I'm passing the data from view1 to view2 to be displayed.
&lt;?php
echo $this->session->userdata($arr);
?&gt;
#7

[eluser]Unknown[/eluser]
hey guy!

you can use the session variables in any view or controller file.

like this:

Code:
&lt;?= $this->session->userdata('name'); ?&gt;

but if you have normal variables or arrays, what ever, you must turn over the data, most suitable, to the controller.

like this:

Controller test.php

Code:
&lt;?php
    class Test extends Controller {
          
        function index()
        {
        
        //You can define such $data variables you want.
        $data['test'] = "";
        
        //Here you load the view file test_output and give over $data
        $this->load->view('test_output.php', $data);
        }
    }

?&gt;

View test_output.php

Code:
Data: &lt;?= $test; ?&gt; and maybe a wee bit <b>html</b>

It`s better to display the informations only in view files and the conditioning of the informations in controllers, models...

greetz chichi




Theme © iAndrew 2016 - Forum software by © MyBB