OldWest Posted January 7, 2011 Share Posted January 7, 2011 I am doing some practice with classes. MY class below echoes out input and radio fields based on the instantiation of the Form class (the code below is working!), but I am trying to resolve how to get my form radio fields so they are not in between every other form field as they are supposed to be next to eachother. If you run the below, you will see what I mean. And also is my approach at all sensible? I am only doing this for learning experience, so any advice please. <?php class Form { private $fields = array(); private $radios = array(); private $actionValue; private $submit = "Submit Form"; private $Nradios = 0; private $Nfields = 0; function __construct($actionValue, $submit) { $this->actionValue = $actionValue; $this->submit = $submit; } function displayForm() { echo "<form action='{$this->actionValue}' method='post'>\n\n"; for ($j = 1, $i = 1; $j <= sizeof($this->fields), $i <= sizeof($this->radios); $j++, $i++) { echo "<p>\n<label>{$this->fields[$j-1]['label']} : </label>\n"; echo "<input type='text' name='{$this->fields[$j-1]['name']}'>\n</p>\n\n"; echo "<p>\n<label>{$this->radios[$i-1]['rlabel']} : </label>\n"; echo "\n<input type='radio' name='{$this->radios[$i-1]['rname']}' value='{$this->radios[$i-1]['rvalue']}'>\n</p>\n\n"; } echo "\n\n<input type='submit' value='{$this->submit}'>\n</form>"; } function addField($name, $label) { $this->fields[$this->Nfields]['name'] = $name; $this->fields[$this->Nfields]['label'] = $label; $this->Nfields = $this->Nfields + 1; } function addRadio($rname, $rvalue, $rlabel) { $this->radios[$this->Nradios]['rname'] = $rname; $this->radios[$this->Nradios]['rvalue'] = $rvalue; $this->radios[$this->Nradios]['rlabel'] = $rlabel; $this->Nradios = $this->Nradios + 1; } } ?> <?php $contact_form = new Form("process.php", "Submit Data >>"); $contact_form->addField("first_name", "First Name"); $contact_form->addField("last_name", "Last Name"); $contact_form->addRadio("gender", "male", "Male"); $contact_form->addRadio("gender", "femail", "Female"); $contact_form->displayForm(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/223652-form-class-radio-field-positioning-issue/ Share on other sites More sharing options...
dragon_sa Posted January 7, 2011 Share Posted January 7, 2011 they are in between because you are looping through each time for each input and each radio i tested this try it this way echo "<form action='{$this->actionValue}' method='post'>\n\n"; for ($j = 1; $j <= sizeof($this->fields); $j++) { echo "<p>\n<label>{$this->fields[$j-1]['label']} : </label>\n"; echo "<input type='text' name='{$this->fields[$j-1]['name']}'>\n</p>\n\n"; } for ($i = 1; $i <= sizeof($this->radios); $i++) { echo "\n<label>{$this->radios[$i-1]['rlabel']} : </label>\n"; echo "\n<input type='radio' name='{$this->radios[$i-1]['rname']}' value='{$this->radios[$i-1]['rvalue']}'>\n\n"; } echo "\n<br/>\n\n<input type='submit' value='{$this->submit}'>\n</form>"; Quote Link to comment https://forums.phpfreaks.com/topic/223652-form-class-radio-field-positioning-issue/#findComment-1156132 Share on other sites More sharing options...
OldWest Posted January 7, 2011 Author Share Posted January 7, 2011 Nice idea. The double for loops seem like the best solution. . Thx. they are in between because you are looping through each time for each input and each radio i tested this try it this way echo "<form action='{$this->actionValue}' method='post'>\n\n"; for ($j = 1; $j <= sizeof($this->fields); $j++) { echo "<p>\n<label>{$this->fields[$j-1]['label']} : </label>\n"; echo "<input type='text' name='{$this->fields[$j-1]['name']}'>\n</p>\n\n"; } for ($i = 1; $i <= sizeof($this->radios); $i++) { echo "\n<label>{$this->radios[$i-1]['rlabel']} : </label>\n"; echo "\n<input type='radio' name='{$this->radios[$i-1]['rname']}' value='{$this->radios[$i-1]['rvalue']}'>\n\n"; } echo "\n<br/>\n\n<input type='submit' value='{$this->submit}'>\n</form>"; Quote Link to comment https://forums.phpfreaks.com/topic/223652-form-class-radio-field-positioning-issue/#findComment-1156255 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.