ian
ian

Reputation: 12335

best method of processing dynamic amount of _POST or _GET variables

I am building a simple scheduling program for week long scheduling: Sunday - Saturday.

Under each day when the schedule is saved there will be a variable amount of employees working and the data for their shift and job code.

For example:

Sunday: Bob - Kitchen - Opening Shift Sam - Kitchen - Closing Shift Amy - Bar - Opening Shift Billy - Bar - Closing Shift

Monday: Bob - Kitchen - Opening Shift Sam - Kitchen - Opening Shift Amy - Bar - Opening Shift

And so on... Thus when the form is submitted I made need to process data for 2 employees. Or 0 employees or 10..

What is the best way to read a dynamic amount of _GET or _POST variables in this situation?

Upvotes: 0

Views: 243

Answers (1)

NSSec
NSSec

Reputation: 4561

You'll want to use POST here, as you're probably modifying said schedule and the amount of data might also exceed the maximum URL limit if you'd use GET.

With that said, PHP can deal with arrays in POST data which you can use to your advantage:

<form method="post">

<input type="hidden" name="schedule[monday][mathieuk]" value="Kitchen" />
<input type="hidden" name="schedule[monday][someuser]" value="Not Kitchen" />
<input type="submit" />

</form>

Would result in $_POST being:

array
  'schedule' => 
    array
      'monday' => 
        array
          'mathieuk' => string 'Kitchen' (length=7)
          'someuser' => string 'Not Kitchen' (length=11)

.. which makes processing it pretty straight forward.

Upvotes: 1

Related Questions