Results 1 to 8 of 8

Thread: Grouping $_Session outputs

  1. #1
    Join Date
    Sep 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Grouping $_Session outputs

    So i have 3 form fields. Each time they are submitted the inputs are display as well as the previous inputs are display. Each time the set of inputs are submitted, each output is put into a div with the same name. That name is increased by one each time. So the first time submit is hit there is 3 <div id="id1"></div> then second submit is hit there are 3 <div id="id1"></div> and 3 <div id="id2"></div> .

    What i need to do is wrap each group of <div id="id(X)"></div>. So if the submit button has been hit twice there should be :
    Code:
    <div id="wrap"> <div id="id1"></div> <div id="id1"></div> <div id="id1"></div> </div> 
    <div id="wrap"> <div id="id2"></div> <div id="id2"></div> <div id="id2"></div> </div>
    Let me know if it would be easier if i show you have i am getting each <div id="id(X)"></div>

  2. #2
    Join Date
    Sep 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Here is my code.. I will make a picture in just a minute...

    PHP Code:
    <?php session_start(); ?>
    <?php
    if(!isset($_SESSION['counter'])){
        
    $_SESSION['counter'] ='1';

    if(
    $_POST)
        
    $_SESSION['name'] .= '<div id="id'.$_SESSION['counter'].'">'.$_POST['name'].'</div>'
        echo 
    $_SESSION['name'] ;
    ?>
    <?php 
    if($_POST)
        
    $_SESSION['email'] .= '<div id="id'.$_SESSION['counter'].'">'.$_POST['email'].'</div>';
        echo 
    $_SESSION['email'] ;  
         
    ?> 
    <?php if($_POST)
        
    $_SESSION['phone'] .= '<div id="id'.$_SESSION['counter'].'">'.$_POST['phone'].'</div>';  
        echo 
    $_SESSION['phone'] ;
         
    ?>    
    <?php if($_POST)
        
    $_SESSION['counter']++;
    ?>
    <?php 
    if($_POST[reset]) {
        unset(
    $_SESSION['counter']);
        unset(
    $_SESSION['name']);
        unset(
    $_SESSION['email']);
        unset(
    $_SESSION['phone']);
     } 
    ?>

         
    <form method="post" action="index.php">
    <div id="name">Name:<input type="text" name="name" size="10" /></div>
    <div id="email">Armor:<input type="text" name="email" size="10" /></div>
    <div id="phone">Strength:<input type="text" name="phone" size="10" /></div>

    <input type="submit" name="submitted" value="Submit"/>
    <input type="submit" name="reset" value="reset" />
    </form>
    And here is a img of what i want to happen...

  3. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    the id needs to be unique on the page: you should never have more than one element with any given id. You might use the class attribute or a custom data attribute (like data-id) instead.

    to be clear, you want the values from each form submission to be displayed, inside the div.wrap, along with all the previous submission values?


    UNTESTED (sorry, not at my server I'll check later but it should work fine)
    PHP Code:
    <?php
    session_start
    ();

    // reset
    if(!empty($_POST['reset'])){
        if(!empty(
    $_SESSION['counter'])){ unset($_SESSSION['counter']); }
        if(!empty(
    $_SESSION['formsubmission'])){ unset $_SESSION['formsubmission']; }
    }

    // is there a submission? process it
    //  (this assumes that you _only_ want to process it if _all_ values are submitted
    //  (e.g., _no_ empty fields allowed
    elseif(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['phone'])){
        
    // is the counter set?
        
    $_SESSION['counter'] = empty($_SESSION['counter'])? 1$_SESSION['counter'];
        
    // escape each input (otherwise you have a security problem)
        
    $name htmlentities($_POST['name']);
        
    $email htmlentities($_POST['email']);
        
    $phonehtmlentities($_POST['phone']);
        
    // save info to session
        // (we will make the html output later)
        
    $_SESSION['formsubmission'][$_SESSION['counter']] = array('name'=>$name,'phone'=>$phone,'email'=>$email);
        
    // increment the counter
        
    $_SESSION['counter']++;
    }

    // markup for the form
    $output '
    <form method="post" action="index.php">
        <div id="name">Name:<input type="text" name="name" size="10" /></div>
        <div id="email">Armor:<input type="text" name="email" size="10" /></div>
        <div id="phone">Strength:<input type="text" name="phone" size="10" /></div>
        <input type="submit" name="submitted" value="Submit"/>
        <input type="submit" name="reset" value="reset" />
    </form>'
    ;

    // make html output if any submissions are saved to session
    if(!empty($_SESSION['formsubmission']){
        foreach(
    $_SESSION['formsubmission'] as $counter => $values){
            
    $html[] = '
    <div class="wrap">
        <div data-count="'
    .$counter.'" data-val="name">Name: '.$values['name'].'</div>
        <div data-count="'
    .$counter.'" data-val="phone">Phone: '.$values['phone'].'</div>
        <div data-count="'
    .$counter.'" data-val="email">Email: '.$values['email'].'</div>
    </div>'
    ;
        }
        
    // here, we put any results at the end of the form
        
    $output .= implode("\n",$html);
    }

    // print it
    print $output;
    ?>
    Last edited by traq; 09-23-2011 at 08:48 PM.

  4. The Following User Says Thank You to traq For This Useful Post:

    deadman6 (09-24-2011)

  5. #4
    Join Date
    Sep 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    correct, i want to be able to position each set of <div id="id(x)">

    or class which i knew it should be just wasnt thinking...

  6. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    if the counter is only for positioning purposes, you should use a class (I used the data- attribute in my example, see above).

  7. #6
    Join Date
    Sep 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by traq View Post
    if the counter is only for positioning purposes, you should use a class (I used the data- attribute in my example, see above).
    srry i was on my iphone and didnt see ur code..

  8. #7
    Join Date
    Sep 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by traq View Post
    the id needs to be unique on the page: you should never have more than one element with any given id. You might use the class attribute or a custom data attribute (like data-id) instead.

    to be clear, you want the values from each form submission to be displayed, inside the div.wrap, along with all the previous submission values?


    UNTESTED (sorry, not at my server I'll check later but it should work fine)
    PHP Code:
    <?php
    session_start
    ();

    // reset
    if(!empty($_POST['reset'])){
        if(!empty(
    $_SESSION['counter'])){ unset($_SESSSION['counter']); }
        if(!empty(
    $_SESSION['formsubmission'])){ unset $_SESSION['formsubmission']; }
    }

    // is there a submission? process it
    //  (this assumes that you _only_ want to process it if _all_ values are submitted
    //  (e.g., _no_ empty fields allowed
    elseif(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['phone'])){
        
    // is the counter set?
        
    $_SESSION['counter'] = empty($_SESSION['counter'])? 1$_SESSION['counter'];
        
    // escape each input (otherwise you have a security problem)
        
    $name htmlentities($_POST['name']);
        
    $email htmlentities($_POST['email']);
        
    $phonehtmlentities($_POST['phone']);
        
    // save info to session
        // (we will make the html output later)
        
    $_SESSION['formsubmission'][$_SESSION['counter']] = array('name'=>$name,'phone'=>$phone,'email'=>$email);
        
    // increment the counter
        
    $_SESSION['counter']++;
    }

    // markup for the form
    $output '
    <form method="post" action="index.php">
        <div id="name">Name:<input type="text" name="name" size="10" /></div>
        <div id="email">Armor:<input type="text" name="email" size="10" /></div>
        <div id="phone">Strength:<input type="text" name="phone" size="10" /></div>
        <input type="submit" name="submitted" value="Submit"/>
        <input type="submit" name="reset" value="reset" />
    </form>'
    ;

    // make html output if any submissions are saved to session
    if(!empty($_SESSION['formsubmission']){
        foreach(
    $_SESSION['formsubmission'] as $counter => $values){
            
    $html[] = '
    <div class="wrap">
        <div data-count="'
    .$counter.'" data-val="name">Name: '.$values['name'].'</div>
        <div data-count="'
    .$counter.'" data-val="phone">Phone: '.$values['phone'].'</div>
        <div data-count="'
    .$counter.'" data-val="email">Email: '.$values['email'].'</div>
    </div>'
    ;
        }
        
    // here, we put any results at the end of the form
        
    $output .= implode("\n",$html);
    }

    // print it
    print $output;
    ?>

    This works perfectly! Thank you so much!!! (except i had to had two sets of ( ) on line 7 and 38? i think..)

    I wasted many hours playing around with this trying to learn how to do it myself

  9. #8
    Join Date
    Sep 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    The only thing i need to change is, letting the inputs =0, So if a field is left blank or is 0 it still display's Name: Email: Phone:

    When i take out
    PHP Code:
    elseif(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['phone'])){ 
    Things can be left blank but they is always a blank set at the begging...

    Name:
    Phone:
    Email:
    PHP Code:
    <div class="wrap">
        <
    div data-count="1" data-val="name">Name'.$values['name'].'</div>
        <
    div data-count="1" data-val="phone">Phone'.$values['phone'].'</div>
        <
    div data-count="1" data-val="email">Email'.$values['email'].'</div>
    </
    div>'; 
    Even when everything else resets...

Bookmarks

Posting Permissions

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