View Full Version : Grouping $_Session outputs
deadman6
09-23-2011, 06:52 PM
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 :
<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>
deadman6
09-23-2011, 08:11 PM
Here is my code.. I will make a picture in just a minute...
<?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...
http://i44.photobucket.com/albums/f38/deadman6gamebattles/Untitled-1copy.jpg
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
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']);
$phone= htmlentities($_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;
?>
deadman6
09-23-2011, 08:43 PM
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...
if the counter is only for positioning purposes, you should use a class (I used the data- attribute in my example, see above).
deadman6
09-23-2011, 08:55 PM
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..
deadman6
09-23-2011, 09:00 PM
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
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']);
$phone= htmlentities($_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 :)
deadman6
09-23-2011, 10:00 PM
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
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:
<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...
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.