eightFX Posted November 16, 2006 Share Posted November 16, 2006 Good morning everyone,I swear I had this script working but all of a sudden it only works with numbers. What I have is a form that has four fields: Title, Area, Section and Division. What I want is if the title does not have an Area, Section, or Division it does not need to combine the strings. Or if it does not have a Section or Division to just combine the Area string, etc all the way down. Here is the code that was working but now only works if I use numerics, but I need to use alphanumeric. Also the form sets default values of each field to 0 . Any help appreciated greatly. Thanks![code]$Area = $_POST['Area'];$Section = $_POST['Section'];$Division = $_POST['Division'];if (($Area != 0) && ($Section != 0) && ($Division != 0)) { $Link = 'Area: '. $Area .'<br>Section: '. $Section .'<br>Division: '. $Division .''; } elseif (($Area != 0) && ($Section != 0) && ($Division == 0)) { $Link = 'Area: '. $Area .'<br>Section: '. $Section .''; } elseif (($Area != 0) && ($Section == 0) && ($Division == 0)) { $Link = 'Area: '. $Area .''; } elseif (($Area == 0) && ($Section == 0) && ($Division == 0)) { $Link = ''; } echo $Link;[/code] Link to comment https://forums.phpfreaks.com/topic/27467-solved-combining-strings-from-form/ Share on other sites More sharing options...
Psycho Posted November 16, 2006 Share Posted November 16, 2006 Well, the problem may be because of your test conditions.I would just use this[code]if ($Area && $Section && $Division) {[/code]However that code is very inefficient. Try this:[code]<?php $Link = ""; if ($Area) { $Link .= "Area: $Area"; } if ($Link && $Section) { $Link .= "<br>Section: $Section"; } if ($Link && $Division) { $Link .= "<br>Division: $Division"; } echo $Link;?>[/code] Link to comment https://forums.phpfreaks.com/topic/27467-solved-combining-strings-from-form/#findComment-125588 Share on other sites More sharing options...
eightFX Posted November 16, 2006 Author Share Posted November 16, 2006 This would work but in my case Area, Section, and Division are always set to something. The default value is set by the form. So I need to set these strings when the default value is not 0. Is this possible? Link to comment https://forums.phpfreaks.com/topic/27467-solved-combining-strings-from-form/#findComment-125590 Share on other sites More sharing options...
Psycho Posted November 16, 2006 Share Posted November 16, 2006 The code I provided will treat 0 the same as if the user entered no value (both are interpreted as false), so you should be good. But, I noticed that in my code if there was a value for area and division, it would combine them both in $Link. Yours does not do that. I have made the corrections below[code]<?php $Link = ""; if ($Area!="") { $Link = "Area: $Area"; if ($Section!="") { $Link .= "<br>Section: $Section"; if ($Division!="") { $Link .= "<br>Division: $Division"; } } } echo $Link;?>[/code]If that doesn't work, post some "test" data and I'll be able to provide a solution. Link to comment https://forums.phpfreaks.com/topic/27467-solved-combining-strings-from-form/#findComment-125593 Share on other sites More sharing options...
eightFX Posted November 16, 2006 Author Share Posted November 16, 2006 I tried your example and it works but I need it to work a bit differently.EX: FORM INPUTAREA: TESTSECTION: 0DIVISION: 0YOUR OUTPUT'Area: TestSection: 0Division: 0'NEEDED OUTPUT'Area: Test'FORM INPUTAREA: 0SECTION: 0DIVISION: 0YOUR OUTPUT'Area: 0Section: 0Division: 0'NEEDED OUTPUT''I hope this helps, let me know. Link to comment https://forums.phpfreaks.com/topic/27467-solved-combining-strings-from-form/#findComment-125601 Share on other sites More sharing options...
Psycho Posted November 16, 2006 Share Posted November 16, 2006 Stupid me, I was correcting my mistake from before and changed the code to work with 0's before I saw your post concerning the fact that 0's were to be treated as no input. I forgot to reverse it back. Again the original condition statements I used is what you want:[code]<?php $Link = ""; if ($Area) { $Link = "Area: $Area"; if ($Section) { $Link .= "<br>Section: $Section"; if ($Division) { $Link .= "<br>Division: $Division"; } } } echo $Link;?>[/code] Link to comment https://forums.phpfreaks.com/topic/27467-solved-combining-strings-from-form/#findComment-125602 Share on other sites More sharing options...
eightFX Posted November 16, 2006 Author Share Posted November 16, 2006 Thank You very much. That works great! Thank you for getting rid of my monstrosity of code that didn't work anyways! Appreciate it!!! Link to comment https://forums.phpfreaks.com/topic/27467-solved-combining-strings-from-form/#findComment-125604 Share on other sites More sharing options...
Psycho Posted November 16, 2006 Share Posted November 16, 2006 Just because I was curious. I found an even more efficient method to accomplish this. All you need is this:[code]<?php $Link = (!$Area) ? "" : "Area: $Area" . ( (!$Section) ? "" : "<br>Section: $Section" . ( (!$Division) ? "" : "<br>Division: $Division" ));?>[/code] Link to comment https://forums.phpfreaks.com/topic/27467-solved-combining-strings-from-form/#findComment-125614 Share on other sites More sharing options...
eightFX Posted November 16, 2006 Author Share Posted November 16, 2006 Works great again. Thanks again I really appreciate it! Link to comment https://forums.phpfreaks.com/topic/27467-solved-combining-strings-from-form/#findComment-125623 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.