colombian Posted August 21, 2007 Share Posted August 21, 2007 For simplicity sake, here is what I have: <?PHP $forms = array ( 'fname' => 'text', 'lname' => 'text') foreach ($forms as $field => $type) { # Dynamic code that creates the form like: $output.= '<input type="text" name="'.$field.'" id="'.$field.'"'; if (isset($_REQUEST[$field])) { $output.= ' value="'.htmlspecialchars($_REQUEST[$field]).'"'; } $output.= ' />'; The main problem, is obviously, that "Lname" becomes the html that the user sees. Some of the fields (like email for example) work fine without changing that, but I wanted to somehow create either a multi-dimensional array to store the values I want the user to see, or something else to get different names to display. I haven't worked with complex arrays, and the examples of multi dimensional arrays I saw were severly lacking, or way too simplistic. This seems like it should be easy, but my attempt thus far have failed. I am using a switch to go between 'text' fields and 'textarea', radio buttons, etc. So I would rather not manually type the 'type'. Ideally something like 'fname', 'First name' => 'text', (but this doesn't work - no proper PHP). OR 'fname' => 'First name' => 'text' and formulate a foreach statement (or while) that gets the appropriate values into the dynamic html code. I tried with 2 arrays, one with types, and one with the different names, but it ended up with multiple outputs of the same form. Any help would be greatly appreciated. Thank you Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted August 21, 2007 Share Posted August 21, 2007 <?php $forms = array ( 'fname' => Array( 'text' => 'First Name' ), 'lname' => Array( 'text' => 'Last name' ) ); ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted August 21, 2007 Share Posted August 21, 2007 do you mean <?php $forms = array ( array ('caption' => 'First name', 'field' => 'fname', 'type' => 'text' ), array ('caption' => 'Last name', 'field' => 'lname', 'type' => 'text' ), array ('caption' => 'Email', 'field' => 'email', 'type' => 'text' ) ); $output = ''; foreach ($forms as $fields) { extract ($fields); $output .= "$caption <input type='$type' name='$field' "; if (isset($_REQUEST[$field])) { $output.= ' value="'.htmlspecialchars($_REQUEST[$field]).'"'; } $output.= " /><br/>\n"; } echo $output; ?> Quote Link to comment Share on other sites More sharing options...
colombian Posted August 21, 2007 Author Share Posted August 21, 2007 Thank you for the prompt reply. I am having difficulty translating the multi dimensional array into a foreach loop. A normal array goes: <?php foreach ($forms as $field => $type) { # do stuff } ?> I attempted several different versions trying to capture the multi dimensional array without luck. How would that work? foreach ($forms as $field => $type => $name) { #this lines errors out Do I need to do something more like: Array changed to: $forms = array( array( 'fname' => "First name", 'type' => "text") array( 'lname' => "Last name", 'type' => 'text') ); for ($row = 0; $row < 3; $row++) { $forms[$row]["fname"] $forms[$row]["First name"] $forms[$row]["text"]; } But I'd like to know how to make the equivalent of the above foreach loop in the array you suggested. Again, thanks a lot for your help. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 21, 2007 Share Posted August 21, 2007 You can nest your foreach loops: <?php $forms = array(array( 'fname' => "First name",'type' => "text"),array( 'lname' => "Last name", 'type' => 'text') ); foreach($forms as $k => $v){ foreach($v as $k2 =>$v2){ echo $k2.' : '.$v2.'<br />'; } } ?> Remember, the whole point of multidimensional arrays is that each element within the array is an array in itself. Therefore, you work with each value of the array as you would any other single dimensional array. Hmm, i wonder if i could have put 'array' into those two sentences anymore often that i did. Quote Link to comment Share on other sites More sharing options...
colombian Posted August 21, 2007 Author Share Posted August 21, 2007 Thank you very much Barand. For further PHP knowledge, what does the "extract ($fields);" command do for this example. I was leaning towards a similar solution in terms of the array, I tried your code for the foreach loop and it worked like a charm. Much appreciated! Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted August 21, 2007 Share Posted August 21, 2007 http://www.php.net/extract Quote Link to comment 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.