Omzy Posted August 16, 2009 Share Posted August 16, 2009 Basically I have a text input form which is built up from an array called $fields: $fields=array( 'name'=>array('Contact Name', '1', '25'), 'company'=>array('Company Name', '1', '25'), 'phone1'=>array('Tel Number 1', '1', '25'), 'email'=>array('Email Address', '1', '35'), 'url'=>array('Website URL', '0', '35'), ); foreach($fields as $key => $value) { echo ' <b>'.$value[0].'</b> <input type="text" name="'.$key.'" value="', isset($_POST[$key]) ? $_POST[$key] : null ,'" size="'.$value[2].'"/> '; } As you can see the VALUE attribute is null upon page load or otherwise populated with its POST value if the form gets posted and has to be re-displayed. This all works fine, however I need to extend the ternary statement to do an 'ELSE>IF' type operation. Basically the default value for the field 'url' needs to be 'http://' upon page load. I have tried several different ways of doing this but it just won't work for me! Any ideas anyone? Link to comment https://forums.phpfreaks.com/topic/170498-elseif-ternary/ Share on other sites More sharing options...
Omzy Posted August 16, 2009 Author Share Posted August 16, 2009 I tried this: value="', !isset($_POST[$key]) && $key=='url' ? 'http://' : isset($_POST[$key]) ? $_POST[$key] : null ,'" But it gives me an error notice on page saying: Undefined index: url in ... How is it undefinded? Link to comment https://forums.phpfreaks.com/topic/170498-elseif-ternary/#findComment-899392 Share on other sites More sharing options...
thebadbad Posted August 16, 2009 Share Posted August 16, 2009 Should work if you change null to ($key == 'url' ? 'http://' : null) in your original script. Nested ternary statements are not very readable though. Link to comment https://forums.phpfreaks.com/topic/170498-elseif-ternary/#findComment-899396 Share on other sites More sharing options...
Omzy Posted August 16, 2009 Author Share Posted August 16, 2009 Cheers mate, that has worked. How come it needs to be put in brackets? Link to comment https://forums.phpfreaks.com/topic/170498-elseif-ternary/#findComment-899399 Share on other sites More sharing options...
thebadbad Posted August 16, 2009 Share Posted August 16, 2009 I'm not sure it's actually needed, but it makes it easier to read. And you're sure the code is interpreted the way you intended. Link to comment https://forums.phpfreaks.com/topic/170498-elseif-ternary/#findComment-899484 Share on other sites More sharing options...
Omzy Posted August 16, 2009 Author Share Posted August 16, 2009 Well if I remove those brackets it won't work lol. So the brackets ARE needed lol. Yeah it's all fine and dandy now :-) Link to comment https://forums.phpfreaks.com/topic/170498-elseif-ternary/#findComment-899489 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.