views:

83

answers:

3

So when I pass my filled arrays to the function createform the form is created with $max_avatars options, but they have no name or value. Why are my arrays becoming null?

<?php
    $avatar_image_name = array('hacker','samurai','cool','happy','thatsnice','angry','tv','bang');
    $avatar_name = array('Hacker','Samurai','Cool','Happy','That\'s nice','Angry','TV','Bang');

    $max_avatars = 6;
     createform($max_avatars, $avatar_image_name, $avatar_name); 
?>

The function createform:

function createform($max_avatars, $avatar_image_name, $avatar_name)
                    {
                        $string ='<table id="formtable">
                            <tr><td class="formtext">Name:</td><td><input type="text" name=name size=25 /></td></tr>            
                            <tr><td class="formtext">Trip:</td><td><input type="text" name=trip size=25 /> (<font style="color: #ff0000">Don\'t use your password</font>)</td></tr>
                            <tr><td class="formtext">Comment</td><td><textarea name=post rows="4" cols="50"></textarea></td></tr>
                            <tr><td class="formtext">Avatar:</td>
                            <td>
                            <select name=avatar>';

                        for($i = 0; $i < $max_avatars; $i++){
                            $string = ($string . '<option value="'.$avatar_image_name[i].'">'.$avatar_name[i].'</option>');
                        }

                        $string = ($string . '</select>
                            </td>
                            <tr><td class="formtext">Extra</td><td>Fortune: <input type="radio" name="extra" value="fortune" /> Poker: <input type="radio" name="extra" value="poker" /></td></tr>
                        </table>
                        <input type=submit value="Submit" id="submitbutton">');

                        echo $string;
                    }
+3  A: 

it's $i, not i :)

for($i = 0; $i < $max_avatars; $i++){
                    $string = ($string . '<option value="'.$avatar_image_name[$i].'">'.$avatar_name[$i].'</option>');
            }
Sirber
thank you very much. I will be accepting this as my answer just as soon as SO allows me.
William
+2  A: 

Your for loop's counter is called $i, but you're using i to index the array within the loop.

Syntactic
+7  A: 

You want $avatar_image_name[$i], not $avatar_image_name[i]. You need the $ in front. Otherwise, PHP assumes you meant "i", and $avatar_image_name["i"] doesn't exist, so it replaces it with null. You should turn display_errors on in your php.ini configuration file—PHP will warn you about this stuff if you do.

Samir Talwar
+1 for warnings
Carson Myers