slotegraafd Posted October 10, 2020 Share Posted October 10, 2020 Hey, So I'm doing a school assignment and my professor wants us to use an array to display forms and I have no idea how to do that. He wants us to create an array of arrays like this: display_form( array( "type" => "text", "name" => "first_name", "value" => "", "label" => "First Name" ), array( "type" => "text", "name" => "last_name", "value" => "", "label" => "Last Name" ), array( "type" => "email", "name" => "email", "value" => "", "label" => "Email" ), array( "type" => "number", "name" => "extension", "value" => "", "label" => "Extension" ) ); But I dont understand how you use that array to actually display the form. Does anyone know how? Quote Link to comment https://forums.phpfreaks.com/topic/311586-how-do-you-use-arrays-to-display-a-form/ Share on other sites More sharing options...
benanamen Posted October 10, 2020 Share Posted October 10, 2020 <!DOCTYPE HTML> <html> <head> <title>Untitled</title> <style type="text/css"> /* <![CDATA[ */ body{ background-color:#e4dab8; } form fieldset{ background-color:#fff9e7; border-width:2px; border-style:solid; border-color:#7c5b47; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:20px 0px 20px 0px; width:350px; position:relative; display:block; padding: 0px 10px 10px 10px; } form fieldset legend{ background-color:#7c5b47; border-width:1px; border-style:solid; border-color:#FFCC99; color:#ffcc99; font-weight:bold; font-variant:small-caps; font-size:110%; padding:2px 5px; margin:0px 0px 10px 0px; position:relative; top: -12px; } form fieldset legend img{ padding:0px 5px 0px 5px; } label{ font-size:80%; display:block; float:left; width:100px; text-align:right; margin:6px 5px 0px 0px; } .button{ background-color:#7c5b47; border-width:1px; border-style:solid; border-color:#FFCC99; font-weight:bold; font-family:Verdana, Arial, Helvetica, sans-serif; } /* ]]> */ </style> </head> <body> <form> <fieldset> <legend>My Array Form Generator </legend> <?php if ($_SERVER['REQUEST_METHOD'] == 'POST'){ } // Form Fields Arrays: Field Title, Field Name, Field Type $formfields = array( array( "First Name", "name_first", "text" ), array( "Last Name", "name_last", "text" ), array( "Username", "username", "text" ), array( "Password", "password", "password" ), ); foreach ($formfields as $key => $value) { echo "<label for='{$value[1]}'>{$value[0]}</label>\n<input id='{$value[1]}' name='{$value[1]} type=\"{$value[2]}' value='' ><br><br>\n\n"; } ?> <input type="submit" name="submit" value="Submit" class="button"> </fieldset> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/311586-how-do-you-use-arrays-to-display-a-form/#findComment-1581817 Share on other sites More sharing options...
cyberRobot Posted October 12, 2020 Share Posted October 12, 2020 On 10/10/2020 at 3:30 PM, slotegraafd said: But I dont understand how you use that array to actually display the form. Does anyone know how? Where are you stuck? Creating the array or using the array to display the form? What have you done so far? What does your current code look like? Quote Link to comment https://forums.phpfreaks.com/topic/311586-how-do-you-use-arrays-to-display-a-form/#findComment-1581829 Share on other sites More sharing options...
slotegraafd Posted October 13, 2020 Author Share Posted October 13, 2020 (edited) On 10/10/2020 at 4:59 PM, benanamen said: <!DOCTYPE HTML> <html> <head> <title>Untitled</title> <style type="text/css"> /* <![CDATA[ */ body{ background-color:#e4dab8; } form fieldset{ background-color:#fff9e7; border-width:2px; border-style:solid; border-color:#7c5b47; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:20px 0px 20px 0px; width:350px; position:relative; display:block; padding: 0px 10px 10px 10px; } form fieldset legend{ background-color:#7c5b47; border-width:1px; border-style:solid; border-color:#FFCC99; color:#ffcc99; font-weight:bold; font-variant:small-caps; font-size:110%; padding:2px 5px; margin:0px 0px 10px 0px; position:relative; top: -12px; } form fieldset legend img{ padding:0px 5px 0px 5px; } label{ font-size:80%; display:block; float:left; width:100px; text-align:right; margin:6px 5px 0px 0px; } .button{ background-color:#7c5b47; border-width:1px; border-style:solid; border-color:#FFCC99; font-weight:bold; font-family:Verdana, Arial, Helvetica, sans-serif; } /* ]]> */ </style> </head> <body> <form> <fieldset> <legend>My Array Form Generator </legend> <?php if ($_SERVER['REQUEST_METHOD'] == 'POST'){ } // Form Fields Arrays: Field Title, Field Name, Field Type $formfields = array( array( "First Name", "name_first", "text" ), array( "Last Name", "name_last", "text" ), array( "Username", "username", "text" ), array( "Password", "password", "password" ), ); foreach ($formfields as $key => $value) { echo "<label for='{$value[1]}'>{$value[0]}</label>\n<input id='{$value[1]}' name='{$value[1]} type=\"{$value[2]}' value='' ><br><br>\n\n"; } ?> <input type="submit" name="submit" value="Submit" class="button"> </fieldset> </form> </body> </html> @benanamen Ah I see, Notice: Undefined offset: 2 in C:\XAMPP\htdocs\Webd3201\salesperson.php on line 83 though do you know what this error means? Edited October 13, 2020 by slotegraafd Quote Link to comment https://forums.phpfreaks.com/topic/311586-how-do-you-use-arrays-to-display-a-form/#findComment-1581856 Share on other sites More sharing options...
cyberRobot Posted October 14, 2020 Share Posted October 14, 2020 16 hours ago, slotegraafd said: Notice: Undefined offset: 2 It sounds like you are trying to use an array key that doesn't exist. Are you using an associative array, like your first post above shows. Or are you using a numeric array, like benanamen's code uses? Your choice will affect how you use the keys within the loop that outputs the form fields. Quote Link to comment https://forums.phpfreaks.com/topic/311586-how-do-you-use-arrays-to-display-a-form/#findComment-1581871 Share on other sites More sharing options...
slotegraafd Posted October 14, 2020 Author Share Posted October 14, 2020 7 hours ago, cyberRobot said: It sounds like you are trying to use an array key that doesn't exist. Are you using an associative array, like your first post above shows. Or are you using a numeric array, like benanamen's code uses? Your choice will affect how you use the keys within the loop that outputs the form fields. Oh so I’m using the array that I have in my code there. (I’m really bad with arrays if you couldn’t tell) I thought that each one in the array corresponded to a number no matter what type of arrays Quote Link to comment https://forums.phpfreaks.com/topic/311586-how-do-you-use-arrays-to-display-a-form/#findComment-1581874 Share on other sites More sharing options...
cyberRobot Posted October 14, 2020 Share Posted October 14, 2020 What does your current code look like? Quote Link to comment https://forums.phpfreaks.com/topic/311586-how-do-you-use-arrays-to-display-a-form/#findComment-1581875 Share on other sites More sharing options...
slotegraafd Posted October 15, 2020 Author Share Posted October 15, 2020 6 hours ago, cyberRobot said: What does your current code look like? // Form Fields Arrays: Field Title, Field Name, Field Type $display_form = array( array( "type" => "text", "name" => "first_name", "value" => "", "label" => "First Name" ), array( "type" => "text", "name" => "last_name", "value" => "", "label" => "Last Name" ), array( "type" => "email", "name" => "email", "value" => "", "label" => "Email" ), array( "type" => "number", "name" => "extension", "value" => "", "label" => "Extension" ), ); foreach ($display_form as $key => $value) { echo "<label for='{$value[1]}'>{$value[3]}</label>\n<input id='{$value[1]}' name='{$value[1]} type=\"{$value[0]}' value='{$value[2]}' ><br><br>\n\n"; } ?> <input type="submit" name="submit" value="Submit" class="button"> </fieldset> </form> Quote Link to comment https://forums.phpfreaks.com/topic/311586-how-do-you-use-arrays-to-display-a-form/#findComment-1581893 Share on other sites More sharing options...
Barand Posted October 15, 2020 Share Posted October 15, 2020 I recommend you read https://www.php.net/manual/en/language.types.array.php Quote Link to comment https://forums.phpfreaks.com/topic/311586-how-do-you-use-arrays-to-display-a-form/#findComment-1581900 Share on other sites More sharing options...
cyberRobot Posted October 15, 2020 Share Posted October 15, 2020 (edited) Adding the following line of code inside your foreach loop may help with understanding what's inside $value: echo '<pre>' . print_r($value, true) . '</pre>'; Edited October 15, 2020 by cyberRobot Quote Link to comment https://forums.phpfreaks.com/topic/311586-how-do-you-use-arrays-to-display-a-form/#findComment-1581904 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.