help with an array to grab value of of unknown number of form fields

I could really use some direction on this as I have been all over looking for help.

I have a class registration form that was built in a php form builder application (emailed their support but no response). The registration form allows you to add multiple/infinitely many students and enter their name and info.

The form seems to be passing all the form variables fine and when I print the results, I get something like this:
[php]
array(4)
{
[“StudentName”]=> array(2) { [0]=> string(12) “John Hancock” [1]=> string(8) “Joe Blow” }
[“StudentPhone”]=> array(2) { [0]=> string(12) “444-444-4444” [1]=> string(12) “555-555-5555” }
[“StudentEmail”]=> array(2) { [0]=> string(16) "[email protected]" [1]=> string(12) "[email protected]" }
[“Student_Info”]=> array(1) { [0]=> string(1) “1” } }
[/php]

How do I pull out the variables the send mail to go thru and grab each value and and send it in a sendmail? Keeping in mind again, that their can be any number of students in the list.

Hi,

I’d first restructure it, now you have:

array(
    'all names' => array( ...names... ),
    'all phones' => array( ...phones... ),
    'all emails' => array( ...emails... ),
    'all infos' => array( ...infos... )
);

What sounds more manageable ( and more logic IMHO ):

array(
    array( 'name' => 'John Hancock', 'phone' => '444-444-4444', 'email' => '[email protected]', 'info' => 1 ),
    array( 'name' => 'Joe Blow', 'phone' => '555-555-5555', 'email' => '[email protected]', 'info' => null ),
    ...etc...
);

For that you need a script:
[php]
$input; // This is the array you described
$output = array(); // And this will be the result

// I assume all students have a name, so I’ll use that list to iterate
while ( $name = array_shift( $input[‘StudentName’] )
{ $result = array();
$result[‘StudentName’] = $name;
$result[‘StudentPhone’] = array_shift( $input[‘StudentPhone’] );
$result[‘StudentEmail’] = array_shift( $input[‘StudentEmail’] );
$result[‘StudentInfo’] = ( count( $input[‘StudentInfo’] ) ? array_shift( $input[‘StudentInfo’] ) : null );
}
[/php]
Now you can simply loop through them and you have all the information ‘per student’:
[php]
foreach ( $output as $student )
{ echo "Name : ". $student[‘StudentName’]. “
\n”;
echo "Phone : ". $student[‘StudentPhone’]. “
\n”;
echo "Email : ". $student[‘StudentEmail’]. “
\n”;
echo "Other info: ". ( is_null( $student[‘Studentinfo’] ) ? ‘None’ : $student[‘Studentinfo’] ). “
\n”;
}
[/php]

I hope that helps a bit,
O.

Oh, I used a sort of shortcut ‘if-then-else’ I see. If you’re not familiar with it, it works like this:
[php]
$pookie = true;
$result = ( $pookie ? “Yup, it’s true” : “Naah man! False!” ); // “Yup, it’s true”
// ( [IF PART] ? [TRUE PART, THEN] : [FALSE PART, ELSE] );
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service