Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post

    Hit a snag.. how to set correct amount of session variables

    Ok, I am using session variables to carry values through several pages of user interaction, however I have just hit a snag wondering how I can set the correct amount of session variables in the second part.

    You see, I already know what fields (form input fields) are on the first page so I can easily set those fields to session variables in the first script; however the second page is dynamic & I will have no idea how many of each field will be there so I can't set them beforehand.

    There could be 1 field, there could be 20.

    The script will of course know how many there are, but I don't see the benifet in that as you would need to include all possible variables in the script anyway for the script to set them if needed.

    Any ideas here?

    Thanks!

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    assuming your variables are POST ?

    just loop through whatever is passed and set the session..

    so in each page have something like ...
    PHP Code:
    <?
    session_start
    ();
    /*post vars to ignore*/
    $ignore=array( 'submit' );
    foreach(
    $_POST as $k=>$v){
      if( !isset( 
    $_SESSION['form_vars'][$k] ) && ! in_array$k $ignore ) ){
         
    $_SESSION['form_vars'][$k]=$v
      }
    }

    print_r($_SESSION['form_vars']);
    ?>
    untested but you see the idea ?
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Yeah.. they are POST.

    Ok I put that in my page but don't really get how it works.

    Also, what is the print_r there for? I know what it does, but why are we using it?

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    Some variables you don't need carried around like say submit from your submit button ..anything in here does not get added to the session.
    $ignore=array( 'submit' ); (and checked with im_array())

    then basically we loop through all the POST variables , if they do not already exist in the $_SESSION['form_vars'] array (and they are not part of the $ignore array) we add them to the $_SESSION['form_vars'] array

    the print_r() is not required as such its just a visual to show you the state of the $_SESSION['form_vars'] as you go along whilst testing.

    so run your forms (with the above at the head of each page) , and on the last page the sesion will have all the unique POST variables that were entered.

    using session variables is nice cos you can also then let users go 'back' and refill your form fields from the session data
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #5
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Ok... so how would I refer to each item in the array?

    PHP Code:
    Array (

    [
    input] => value1
    [input2] => value2
    [input3] => value3
    [input4] => value4
    [input5] => value5 
    That's the type of output I get using this.

    So is the arry stored in $ignore?

    PHP Code:
    $ignore[input1
    ..or are we making use of the $v variable?

  • #6
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Looking at it again, I assume we use something in the form of this..

    PHP Code:
    $_SESSION['form_vars'][$v
    ..or something.

  • #7
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    I got it figured out heh..

    PHP Code:
    $_SESSION['form_vars']['variable']; 

  • #8
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Ok, ran into a problem here with this code you gave me..

    PHP Code:
    <? 
    session_start
    (); 
    /*post vars to ignore*/ 
    $ignore=array( 'submit' ); 
    foreach(
    $_POST as $k=>$v){ 
      if( !isset( 
    $_SESSION['form_vars'][$k] ) && ! in_array$k $ignore ) ){ 
         
    $_SESSION['form_vars'][$k]=$v;  
      } 


    print_r($_SESSION['form_vars']); 
    ?>
    Some new code of mine that depended on getting these session variables wasn't working & I couldn't work out why so I have been going through everything & found a problem with this code..

    It brings the values of the variables from the last page, however, if something already has a current value it will use that as the value rather than the input from the user.

    I have some fields where I have some default values in them & some which don't... I outputted the results to the screen & it displays the correct values for each field I didn't supply a default value for but outputs the default value for all boxes I did supply default values no matter what I enter into these fields.

    Any help?

    Thanks a lot!

  • #9
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    ok , if you want to be able to overwrite the defaults ... change this

    if( !isset( $_SESSION['form_vars'][$k] ) && ! in_array( $k , $ignore ) ){

    to this

    if( ! in_array( $k , $ignore ) ){

    in otherwords remove the bit that checks if the value already exists in the session
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #10
    Regular Coder
    Join Date
    Apr 2004
    Posts
    684
    Thanks
    24
    Thanked 1 Time in 1 Post
    Thanks! Works good!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •