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

hypervisorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.