Greetings, I have a question: my webapplication requires to show the same form a given number of times, and ask for different data each time, as in, the form calls itself over and over until necessary. The data from these distinct compilations is then used to fill a PDF template, valid for every dataset submitted, and output the result. I am currently using session arrays to store the datasets from which the PDF forms are then compiled.
What I need to accomplish is to identify each distinct array generated, and since I don't know a priori the number of documents the user will need to generate, keep count of the various datasets progressively.
What I'm thinking of is using a counter variable to keep tabs on the proper array, and generate their names dynamically (as in, ${$arrName.$i} = $foo);
however, I also have a mechanism that allows the user to push the back button and retain previously inputted form data in all of the other pages, so I can't just have an upwards counter -- that would mean losing reference to the previous arrays in the set.
So, I was considering the usage of $_GET
variables, and pass the form number 开发者_运维百科through the URL, allowing me to recognize every requested version of the page independently from the rest, but this seems like a kludge to me; however, this would eradicate the necessity to keep track of all subsequent arrays code-wise, and sort of facilitate things for me.
May I have an opinion on this method, and some insight on a better solution, if any? Please note that the application only relies on itself, and doesn't have a database to use as an exchange point, which would marginally facilitate things. Thanks in advance for any constructive response.
Do you want to do something like a wizard? If that is what you want then it means you already know how many steps are there so you actually render a different form for each step of the wizard.Sending the step number via get is the best solution. You need still to keep track of it's data. I think I would do something like this:
class Wizard {
private $fromsData;/*array with forms*/
private $steps;
private $wizards;
private $lastStep;//last completed step
private function Wizard() {
$this->lastStep = 0;
}
public static function getInstance($name) {
if(isset($_SESSION["wizards"]["name"]) && !isset(self::wizards[$name])) {
self::wizards[$name] = unserialize($_SESSION["wizards"]["name"]);
unset($_SESSION["wizards"]["name"]);
}elseif(!isset(self::wizards[$name])) {
self::wizards[$name] = new Wizard();
}
return self::wizards[$name];
}
public static function commitWizard($name) {
$_SESSION["wizards"]["name"] = serialize(self::wizards[$name]);
}
public function addData($data,$step = null){
if(is_null($step)) {
$this->steps++;
$step = $this->steps;
}
$this->formsData[$step] = $data;
$this->lastStep = $step;
return $step+1;
}
public function getSteps() {
return $this->steps;
}
public function getData($step) {
return $this->formsData[$step];
}
}
This is a class that might help you manage your forms data and steps. It keeps the data per step serialized in the session so you can get it later on and also jump between steps.
a use might look like this;
$wizardName = "sample";
$myWizard = Wizard::getInstance($wizardName);
$step = isset($_POST["step"]) ? $_POST["step"] : null;
if(isset($_POST["data"])) {
if(myValidationFor($_POST["data"]) {
$step = $myWizard->addData($_POST["data"],$step);
} else {
$step =$myWizard->getLastStep();
}
}
if(!is_null($step)) {
$dataForForm = $myWizard->getData($step);//data To Be Shown in the form
};
$showStep = $step+1;
Wizard::commitWizard($wizardName);//saves it into the session
}
I do not know if this is functional "As is" but I think you get the main idea in order to make further use of it. In the template/html area you can have ifs or a switch that changes the fields of the form according to the step you are into. The idea is to navigate easily through the steps of the wizard keeping the date and the last step saved. Considering that you have know the last step, instead of passing the step number you can just pass the back (meaning:decrease the last step or forward meaning increase the last step).
Hope this helps.
Although it depends on what you need to do, you should know that a registration form must have at most 3 steps.
精彩评论