Jump to content

SOLVED: Combining Strings from Form


eightFX

Recommended Posts

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

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]
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.
I tried your example and it works but I need it to work a bit differently.
EX:
FORM INPUT
AREA: TEST
SECTION: 0
DIVISION: 0

YOUR OUTPUT
'Area: Test
Section: 0
Division: 0'

NEEDED OUTPUT
'Area: Test'


FORM INPUT
AREA: 0
SECTION: 0
DIVISION: 0

YOUR OUTPUT
'Area: 0
Section: 0
Division: 0'

NEEDED OUTPUT
''

I hope this helps, let me know.
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]
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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.