|  | Posted by Rory Browne on 05/13/05 03:30 
Personally I'm lazy, but I'd probably go with something along the lines of 
 $filename = sprintf("step%d.php", (int)($_SESSION['step']) );
 require ( file_exists($filename) ? $filename : "step1.php" );
 
 same results in two lines of code - was one line, but I split it into
 two lines to make it more readable, although tbh in production code,
 I'd add an array_key_exists, to make sure that $_SESSION['step'],
 actually exists. It also scales up to as many steps as you like, so
 long as they're all in the stepNUMBER.php format. I don't think there
 is any need for both a %d formatter and (int) typecasting, but it's
 generally best to take every reasonable precaution when you're
 including/requireing files.
 
 On 5/12/05, dan <info@hostinthebox.net> wrote:
 > Hello, all -
 >
 > I've been researching how to handle forms properly, and I think I
 > figured out a way that might be beneficial for me to use.  It is as follows:
 >
 > (index.php)
 >
 > session_start();
 > if (isset($_SESSION['step'])) {
 >         switch $_SESSION['step'] {
 >                 case "1":
 >                         require('step1.php');
 >                         break;
 >                 case "2":
 >                         require('step2.php');
 >                         break;
 >                 case "3":
 >                         require('step3.php');
 >                         break;
 >                 // add more case statements here if I need to
 >                 default:
 >                         require('step1.php');
 >                         break;
 >         }
 > } else {
 >         $_SESSION['step'] = '1';
 >         require('step1.php');
 > }
 >
 > Each stepX.php file would look something similar to this:
 >
 > (step1.php)
 >
 > // if submitted, check data for completeness
 > // if complete, set 'step' to 2, to be used as argument to index.php
 >         $_SESSION['step'] = '2'
 > // redirect back to index.php, use new value of 'step' to direct
 >         header('Location: http://somesite.com/index.php');
 > // else display form data
 >
 > Now, this is, really, one of my first experiences with doing forms.  I
 > just want to know if I can/should/would anticipate any problems down the
 > road while doing this.  I think it would work quite well, but I've only
 > been doing this for a short while.
 >
 > Thanks!
 > -dant
 >
 > --
 > PHP General Mailing List (http://www.php.net/)
 > To unsubscribe, visit: http://www.php.net/unsub.php
 >
 >
  Navigation: [Reply to this message] |