2

I have a form that I created with rows of input fields that are created dynamically.

foreach ($data as $k => $v){
print "<tr>Table # $k</tr>";
print "<tr><td>First Name</td><td>Last Name</td></tr>"

    for($i = 1; $i <= $data[$k]['tickets']; $i++){
        print "<tr><td><input name='fname[]' type='text' class='mid_text_data'  value=".$prefill[$k]['first_name']." maxlength='15' /></td><td><input name='lname[]' type='text' class='mid_text_data'  value=".$prefill[$k]['last_name']." maxlength='15' /></tr>";
    }
}

I've created the input fields based on the number of 'tickets' in my $data array, but I would like to prefill the values of fname with $prefill[$k]['first_name'].

My $prefill and $data variables look like:

$prefill = {
[145]=> array(9) { 
    ["first_name"]=> string(5) "John" 
    ["last_name"]=> string(6) "Doe" 
    ["company"]=> string(10) "Big Business" 
    ["email"]=> string(21) "[email protected]" 
    ["photo_op"]=> int(0) 
    ["gala"]=> int(1) 
    ["chairman"]=> int(0) 
    ["prefix"]=> NULL 
    ["table_number"]=>int(60)
} 
[146]=> array(9) { 
    ["first_name"]=> string(5) "Jane" 
    ["last_name"]=> string(6) "Doe" 
    ["company"]=> string(10) "Big Business" 
    ["email"]=> string(21) "[email protected]" 
    ["photo_op"]=> int(0) 
    ["gala"]=> int(1) 
    ["chairman"]=> int(0) 
    ["prefix"]=> NULL 
    ["table_number"]=>int(60)
} 
[147]=> array(9) { 
    ["first_name"]=> string(5) "Stan" 
    ["last_name"]=> string(6) "Derp" 
    ["company"]=> string(10) "Big Business" 
    ["email"]=> string(21) "[email protected]" 
    ["photo_op"]=> int(0) 
    ["gala"]=> int(1) 
    ["chairman"]=> int(0) 
    ["prefix"]=> NULL 
    ["table_number"]=>int(83)
} 

}

$data = { 
[60]=> array(5) { 
    ["tickets"]=> int(10) 
    ["gala"]=> int(8) 
    ["chair"]=> int(2) 
    ["ind_id"]=> float(805879) 
    ["photo"]=> int(0) 
} 
[83]=> array(5) { 
    ["tickets"]=> int(5) 
    ["gala"]=> int(5) 
    ["chair"]=> NULL 
    ["ind_id"]=> float(805879) 
    ["photo"]=> int(1) 
} 

}

Currently, the form repeats 'John' to each one of my 'fname' fields.

Any ideas how to prefill the fields properly?

Thank you.

1
  • Side note: $data[$k]['tickets'] can just be $v['tickets'] in this case, for brevity. Commented Jul 25, 2011 at 19:00

3 Answers 3

1

The relationship between $prefill and $data seems to be like a "has-one". Couldn't you merge these two arrays for a simpler manipulation ?

For example, if you only use tickets from $data, why not putting it into $prefill directly (for instance, directly in your SQL query).

$prefill = {
[1]=> array(9) { 
    ["first_name"]=> string(5) "John" 
    ["last_name"]=> string(6) "Doe" 
    ["company"]=> string(10) "Big Business" 
    ["email"]=> string(21) "[email protected]" 
    ["photo_op"]=> int(0) 
    ["gala"]=> int(1) 
    ["chairman"]=> int(0) 
    ["prefix"]=> NULL 
    ["table_number"]=>int(60), 
    ["tickets"]=> int(10) 
}

If you need all the $data's data, just nest $data parts in the corresponding $prefill parts :

$prefill = {
[1]=> array(9) { 
    ["first_name"]=> string(5) "John" 
    ["last_name"]=> string(6) "Doe" 
    ["company"]=> string(10) "Big Business" 
    ["email"]=> string(21) "[email protected]" 
    ["photo_op"]=> int(0) 
    ["gala"]=> int(1) 
    ["chairman"]=> int(0) 
    ["prefix"]=> NULL 
    ["table_number"]=>int(60), 
    ["data"]=> array(5) { 
        ["tickets"]=> int(10) 
        ["gala"]=> int(8) 
        ["chair"]=> int(2) 
        ["ind_id"]=> float(805879) 
        ["photo"]=> int(0) 
    } 
} 
Sign up to request clarification or add additional context in comments.

Comments

0

You're using $i as the inner loop, but are using $k in your array references. Try this:

for($i = 1; $i <= $data[$k]['tickets']; $i++) {
    echo <<<EOL
<tr>
    <td>
        <input name="fname[]" type="text" class="mid_text_data" value="{$prefill[$i]['first_name']}  maxlength='15' /></td>
    <td>
        <input name="lname[]" type="text" class="mid_text_data" value="{$prefill[$k]['last_name']}" maxlength="15" />
</tr>
EOL;

}

Note the use of a HEREDOC - far easier to read the html like this, instead of having it all smooshed into a single line of text.

However, to be strictly correct, you should pass actually be using htmlspecialchars() to escape any HTML metacharacters in your prefill array - otherwise an extra " or ' or > could totally hose your form.

2 Comments

Thanks Marc, but the offset of $i will not match up with the keys of $prefill. Excuse the phantom edit, but prefill's keys are IDs of the attendees (unique).
I went back and looked at the code that was constructing my $prefill value, and it wasn't even using any type of unique ID for the array. What I ended up doing was as I was creating the $prefill array, I made my own keys that will correspond with $i. THANKS!
0

Instead of $prefill[$k] can you try $prefill[i]

1 Comment

Thanks for the resposne. $i would work if my keys for $prefill were actually an incrementing counter that my for loop is using. They are actually arbitrary IDs in the database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.