p3nguattack Posted August 29, 2012 Share Posted August 29, 2012 Okay, so I'm trying to make a function to automatically make forms for me based on values I have in 5 arrays(all the requirements I have for the forms). The one I've made works, but only so long as I have exactly 5 values in each array. As I said in the title I need a better way to do this. I've tried foreaching each of the arrays and that gives me forms, but WAY more than what I want. Like 50-60 instead of 5... Anyway look at this code, and I'm sure you'll think I'm stupid for doing it this way lol, but I couldn't think of another way to populate a function with 5 variables that actually worked as I wanted it to. public function formarray() { $names0 = $this->names[0]; $names1 = $this->names[1]; $names2 = $this->names[2]; $names3 = $this->names[3]; $names4 = $this->names[4]; $titles0 = $this->titles[0]; $titles1 = $this->titles[1]; $titles2 = $this->titles[2]; $titles3 = $this->titles[3]; $titles4 = $this->titles[4]; $types0 = $this->types[0]; $types1 = $this->types[1]; $types2 = $this->types[2]; $types3 = $this->types[3]; $types4 = $this->types[4]; $values0 = $this->values[0]; $values1 = $this->values[1]; $values2 = $this->values[2]; $values3 = $this->values[3]; $values4 = $this->values[4]; $lengths0 = $this->lengths[0]; $lengths1 = $this->lengths[1]; $lengths2 = $this->lengths[2]; $lengths3 = $this->lengths[3]; $lengths4 = $this->lengths[4]; $formarray = array( 0 => array( 'title' => $titles0, 'name' => $names0, 'type' => $types0, 'value' => $values0, 'length' => $lengths0 ), 1 => array( 'title' => $titles1, 'name' => $names1, 'type' => $types1, 'value' => $values1, 'length' => $lengths1 ), 2 => array( 'title' => $titles2, 'name' => $names2, 'type' => $types2, 'value' => $values2, 'length' => $lengths2 ), 3 => array( 'title' => $titles3, 'name' => $names3, 'type' => $types3, 'value' => $values3, 'length' => $lengths3 ), 4 => array( 'title' => $titles4, 'name' => $names4, 'type' => $types4, 'value' => $values4, 'length' => $lengths4 )); foreach($formarray as $key => $values) { $title = $values['title']; $name = $values['name']; $type = $values['type']; $value = $values['value']; $length = $values['length']; $this->textfield_t($title, $name, $type, $value, $length); } } and here's the function it populates, just so you can see that too. public function textfield_t($title, $name, $type, $value="", $length=35) { echo '<tr> <td>'.$title.':</td><td><input name="'.$name.'" type="'.$type.'" value="'.@$value.'" maxlength="'.$length.'" /><br /></td> </tr>'; } As I'm sure you can see, I really do need a better way to do this. I'm sure this is ridiculous. I tried calling the array values directly into the $formarray, but it kept giving me syntax errors which I have yet to discover the cause of. It's a pretty handy function, but it could be a lot better, and would be if I didn't have to have exactly 5 values in each of the arrays. If it was more dynamic. I realize I could just put if statements in front of them all, and if that's my only option I'll gladly do so, but I'd like to have some other opinions first. Thanks everyone for your time. Quote Link to comment https://forums.phpfreaks.com/topic/267730-i-need-a-better-way-to-do-this/ Share on other sites More sharing options...
Psycho Posted August 29, 2012 Share Posted August 29, 2012 You aren't showing how $this->names, $this->types, etc. are defined. I don't know why you would define those into separate arrays instead of a multi-dimensional array. But,with what you have I would think this would work - assuming you require a title for each record public function formarray() { foreach($this->titles as $id => $title) { $this->textfield_t($title, this->names[$id], $this->types[$id], $this->values[$id], $this->lengths[$id]); } } public function textfield_t($title, $name, $type, $value="", $length=35) { echo "<tr>\n"; echo "<td>{$title}:</td><td><input name='{$name}' type='{$type}' value='@{$value}' maxlength='{$length}' /><br /></td>\n"; echo "</tr>\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/267730-i-need-a-better-way-to-do-this/#findComment-1373488 Share on other sites More sharing options...
p3nguattack Posted August 29, 2012 Author Share Posted August 29, 2012 I have them defined as single arrays so I can set the values on the page I need to call the function(I'm not very good with arrays). Your function worked great(after only one syntax error XP), though because of my ignorance of arrays, I'm assuming that the $id, is just referencing the keys from each array. With that said, would this still work if I used one array without equal values? I'll show you my arrays first. $form->names =array('name', 'email', 'username', 'password'); $form->titles =array('name', 'email', 'username', 'password'); $form->types =array('text', 'text', 'text', 'password'); $form->values =array('joe', 'matt', 'steve', 'larry'); $form->lengths =array('22', '33', '14', '43'); This was just a test while I was trying to make the function, not a real form obviously, but suppose I took the last two lengths out, would it still call the first two and leave the default for textfield_t($length=35)? I'm thinking it'll throw undefined index errors at me for the [2] and [3] that the $id stands for, or possibly give me a blank value unless I use array push? Of course that wouldn't matter on the lengths. My formarray function was originally just supposed to put them into a multidimensional array, because for some reason I couldn't figure out the syntax when trying to make it just a variable, but I figured if I was doing that, why not just run the textfield_t at the same time, since that's the only reason I'd use that array. I appreciate your help, you shortened that at least 400% lol. Quote Link to comment https://forums.phpfreaks.com/topic/267730-i-need-a-better-way-to-do-this/#findComment-1373712 Share on other sites More sharing options...
Psycho Posted August 29, 2012 Share Posted August 29, 2012 If you do not have values for each record, then yes, it will throw an undefined index value. But, that would be correct behavior in my opinion because you would be throwing crap data to the function. Your Arrays should not be constructed like that anyhow. Trying to associate values across different arrays like that is too difficult to manage. You should define the arrays something like this: $formFields = array( array('name' => 'name', 'title'=>'Name', 'type'=>'text', 'value'=>'', 'length'=>'22'), array('name' => 'email', 'title'=>'Email', 'type'=>'text', 'value'=>'', 'length'=>'33'), array('name' => 'username', 'title'=>'Username', 'type'=>'text', 'value'=>'', 'length'=>'14'), array('name' => 'password', 'title'=>'Password', 'type'=>'password', 'value'=>'', 'length'=>'43') ); Then you can easily repurpose your function if you were to store the data for the form fields in a database. Quote Link to comment https://forums.phpfreaks.com/topic/267730-i-need-a-better-way-to-do-this/#findComment-1373719 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.