stevewar Posted March 9, 2007 Share Posted March 9, 2007 Your help is greatly appreciated: I have the need to take this static code and turn it into a function that takes the input "name" as an argument and echos out this entire bit of code with only the input "name" changed. The reason is that there are 28 different inputs and I don't want to repeat this code all over the place. If there is a better solution please feel free. Thanks! <input name="tricepsSkinfold1" type="text" size="10" maxlength="2" value=" <?php if( isset( $_SESSION['bValidated']['tricepsSkinfold1']['value'] ) ) { echo $_SESSION['bValidated']['tricepsSkinfold1']['value']; } ?>" <?php if( -1 == $_SESSION['bValidated']['tricepsSkinfold1']['valid'] ) { echo "style=\"background:#FFFF00\""; } ?> /> <?php if( -1 == $_SESSION['bValidated']['tricepsSkinfold1']['valid'] ) { echo "<br/>".$_SESSION['errors']['tricepsSkinfold1']; } ?> Link to comment https://forums.phpfreaks.com/topic/42015-echo-problems/ Share on other sites More sharing options...
per1os Posted March 9, 2007 Share Posted March 9, 2007 Damn dude talk about a hard way of doing stuff. Maybe this will help. <?php echo createInput('tricepsSkinfold1'); function createInput($name) { $return = '<input name="'.$name.'" type="text" size="10" maxlength="2"'; if (isset($_SESSION['bValidated'][$name]['value'])) { $return .= 'value="'.$_SESSION['bValidated'][$name]['value'] . '" '; }else { $return .= 'value="" '; } if (isset($_SESSION['bValidated'][$name]['valid']) && $_SESSION['bValidated'][$name]['valid'] == -1) { $return .= 'style="background: #FFFF00" ' . $_SESSION['errors'][$name]; } return $return . ' />'; } ?> Then if you have an array of the names, just loop them with that function. Simple as that. --FrosT Link to comment https://forums.phpfreaks.com/topic/42015-echo-problems/#findComment-203755 Share on other sites More sharing options...
Barand Posted March 9, 2007 Share Posted March 9, 2007 or <?php function print_input ($name) { $value = isset( $_SESSION['bValidated'][$name]['value'] ) ? $_SESSION['bValidated'][$name]['value'] : ''; $style = $error = ''; if( -1 == $_SESSION['bValidated'][$name]['valid'] ) { $style = "style=\"background:#FFFF00\""; $error = $_SESSION['errors'][$name]; } return "<input name=\"$name\" type=\"text\" size=\"10\" maxlength=\"2\" value=\"$value\" $style /> $error"; } echo print_input('tricepsSkinfold1') ; ?> Link to comment https://forums.phpfreaks.com/topic/42015-echo-problems/#findComment-203763 Share on other sites More sharing options...
per1os Posted March 9, 2007 Share Posted March 9, 2007 Theres more than one way to skin a cat =) --FrosT Link to comment https://forums.phpfreaks.com/topic/42015-echo-problems/#findComment-203765 Share on other sites More sharing options...
stevewar Posted March 9, 2007 Author Share Posted March 9, 2007 Thanks guys. I'm not really a php developer. Actually I'm a nutritionist trying to develop php applications, and I just started messing with it this week. I've got user management working and tested, now I'm working on this progress tracker application. I appreciate your help as it will improve my efficiency! -Steve Link to comment https://forums.phpfreaks.com/topic/42015-echo-problems/#findComment-203776 Share on other sites More sharing options...
Barand Posted March 9, 2007 Share Posted March 9, 2007 Theres more than one way to skin a cat =) --FrosT I'd written it as you were posting so as the style was significantly different I thought I may as well post the alternative. BTW, the error message should be outside the input field tag Link to comment https://forums.phpfreaks.com/topic/42015-echo-problems/#findComment-203786 Share on other sites More sharing options...
per1os Posted March 9, 2007 Share Posted March 9, 2007 Ah, its all good =) Either way there is never 1 right answer to any question in coding. There are always 10 different ones =) --FrosT Link to comment https://forums.phpfreaks.com/topic/42015-echo-problems/#findComment-203792 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.