Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

PHP multiple variables

I would like the second $faxnumber variable to only be equal to the $faxnumberX variables that are NOT NULL.  If they are NULL, then I don't want them included in the $faxnumber variable.  I also do not want the commas to be included if there aren't variables to seperate.  Any ideas?

// import form information 
$faxnumber = $_POST['faxnumber'];
$faxnumber2 = $_POST['faxnumber2'];
$faxnumber3 = $_POST['faxnumber3'];
$faxnumber4 = $_POST['faxnumber4'];
$faxnumber5 = $_POST['faxnumber5'];
$faxnumber6 = $_POST['faxnumber6'];

$name        = $_POST['name'];
$faxnumber    = "$faxnumber;$faxnumber2;$faxnumber3;$faxnumber4;$faxnumber5;$faxnumber6";
$message    = $_POST['message'];
 

Open in new window

0
hypervisor
Asked:
hypervisor
1 Solution
 
haloexpertsexchangeCommented:
assuming only 6 $faxnumber variables as all you will have

for ($i=2;$i<=6;$i++)
{
$fax = "faxnumber"+i;
if(!is_null($$fax))
$faxnumber .= ",".$$fax;
}

Open in new window

This should work
0
 
nemws1Database AdministratorCommented:
Just an FYI ... this is a PHP question, but you posted it to the Perl Programming zone.

That being said, you can use some simple if() statements - change line 10 of your code to the following lines:
if ($faxnumber2) { $faxnumber = "$faxnumber;$faxnumber2"; };
if ($faxnumber3) { $faxnumber = "$faxnumber;$faxnumber3"; };
if ($faxnumber4) { $faxnumber = "$faxnumber;$faxnumber4"; };
if ($faxnumber5) { $faxnumber = "$faxnumber;$faxnumber5"; };
if ($faxnumber6) { $faxnumber = "$faxnumber;$faxnumber6"; };

Open in new window

0
 
hypervisorAuthor Commented:
haloexpertsexchange, can you show me your code integrated with mine?  Curious how it fits in...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
haloexpertsexchangeCommented:
it is for after you have gotten the post values into their individual variables then you a basically just looking through them to see if there is anything in them to add to the $faxnumber string.
sorry was not paying enough attention and had the string have , in between instead of the ; that you showed in your example.
0
 
CKY092Commented:
The following code will satisfy your needs, however you'll need to ensure that the first fax number that comes through via $_POST has a 1, such as $_POST['faxnumber1'].
// Assuming 6 Fax Numbers
for($i = 1; $i <= 6; $i++){
    if($_POST['faxnumber' . $i] != null){
        if($i > 1){ $faxnumber .= ","; } // Add comma if needed
	$faxnumber .= $_POST['faxnumber' . $i]; 
    }
}

Open in new window

0
 
CKY092Commented:
I noticed you posted another question about this, but in that example, the fax numbers weren't actually null, they were 0000000000. In that case, the following code, should suffice.
// Assuming 6 Fax Numbers
for($i = 1; $i <= 6; $i++){
    if($_POST['faxnumber' . $i] != '0000000000'){
        if($i > 1){ $faxnumber .= ","; } // Add comma if needed
	$faxnumber .= $_POST['faxnumber' . $i]; 
    }
}

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now