PHP if statement...

I have a form that can potentially have a field value of "None".  When that happens, I want the value to go to null.  I was thinking an "if" statement, but all I can find is syntax for if/else, or if/echo (or print).  I really just need if/'change the value to nothing'.

I hope you can help!

code_red
code_redAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Ray PaseurCommented:
If you mean the value can be the text string "None" you can test for this and set the value to NULL.  Example:

if ($_POST['thing'] == 'None') $_POST['thing'] = NULL;

Does that make sense?
TobiasCommented:
Dear,

This code should do the work :


<?php
If($value=="None")
{
$value = NULL;
}
?>

Open in new window


Regards
code_redAuthor Commented:
Thank you both! I cannot get either of these to work.  Where should I place the code?  I've tried both on the form and the formmail script.  They both seem they should do the trick though.

Editing to post my code...

Form page has an array, 4 classes for each student.  Here is the code for the array...

<?php
$Student1Classes = array('1Class1', '1Class2', '1Class3', '1Class4');
$Student1Courses = implode(", ", $Student1Classes);
?>

If no classes are picked, the value of $Student1Classes is "None, None, None, None".  I'd like for nothing to show if that is the case.

Thank you!
code_red
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Ray PaseurCommented:
Please post the part of the form page that creates the input statements in question.  I will add the action script code to show you how to process it.
TobiasCommented:
Dear,


foreach ($Student1Classes as &$classes) {
If($classes=="None")
{
$Student1Classes [$classes] = NULL; 
}

}

Open in new window


Regards

//Corrected forgot to update in the array
code_redAuthor Commented:
Ray, I hope this is what you are asking for...and thank you.

          <select name="Student1Classes[]" id="1Class1">
            <option selected>None</option>
            <option selected>Math</option>
            <option selected>Science</option>
           </select>

          <select name="Student1Classes[]" id="1Class2">
            <option selected>None</option>
            <option selected>English</option>
            <option selected>Spanish</option>
           </select>

<?php
$Student1Classes = array('1Class1', '1Class2');
$Student1Courses = implode(", ", $Student1Classes);
?>

If nothing is selected, it posts, "None, None".  I'd just like a blank value if that is the case.

code_red
Ray PaseurCommented:
That's great.  I'll be back in few moments.
Ray PaseurCommented:
Experiment with this a little bit, then post back if you still have questions.
http://www.laprbass.com/RAY_temp_code_red.php

HTH, ~Ray
<?php // RAY_temp_code_red.php
error_reporting(E_ALL);
echo "<pre>";

// IF THE FORM HAS BEEN SUBMITTED
if (!empty($_POST))
{
    // ACTIVATE THIS TO SHOW THE POSTED DATA
    // var_dump($_POST);

    // SANITIZE AND PROCESS THE ARRAY
    $chosen = array();
    foreach ($_POST['Student1Classes'] as $class)
    {
        if (empty($class)) continue;
        $chosen[] = $class;
    }
    $classes = implode(',', $chosen);

    // SHOW THE WORK PRODUCT
    echo PHP_EOL;
    echo "YOU CHOSE: $classes";
}

// USE HEREDOC NOTATION TO CREATE THE HTML FORM
$form = <<<FORM
<form method="post">
<select name="Student1Classes[]" id="1Class1">
<option value="" selected>None</option>
<option value="Math">Math</option>
<option value="Science">Science</option>
</select>

<select name="Student1Classes[]" id="1Class2">
<option value="" selected>None</option>
<option value="EN">English</option>
<option value="ES">Spanish</option>
</select>

<input type="submit" />
</form>
FORM;

echo $form;

Open in new window

code_redAuthor Commented:
This is great, the only thing is that my form results are sent via email, not just posted back on the same page.  How could I modify this to make that happen?

code_red
Ray PaseurCommented:
Collect the results into a variable and use the variable in the construction of the email message.  See line 18 where we collect the results into a variable.
code_redAuthor Commented:
I got it to work, but this still seems to submit the results as "None, None", if nothing is chosen.  I'd like it to submit an empty variable if nothing is chosen.  Does that make sense?
Ray PaseurCommented:
The objective makes sense, but if you have a script that is not producing the output you want, you will need to modify the script.  You might need to modify the HTML form (I did).
code_redAuthor Commented:
Ok, let me try this...I have to have the value= "None" for the ones they don't choose since they are for different class periods.  That way, if they don't have a class chosen for 1st period, but do have one for 2nd, then it will look like: "None, Spanish".  Not: "Spanish".

Otherwise, this works just like my previous code.

I'm going to try to re-word my initial request here:  

I have a form that can potentially have a field value (through an array) of "None, None".  When that happens, I want the value to be deleted.  If the value from the array is "None, Spanish", or "Math, None", I want to keep it.  I was thinking an "if" statement, but all I can find is syntax for if/else, or if/echo (or print).  I really just need if value="None, None"/'change the value to nothing'.

<code>:
          <select name="Student1Classes[]" id="1Class1">
            <option selected>None</option>
            <option selected>Math</option>
            <option selected>Science</option>
           </select>

          <select name="Student1Classes[]" id="1Class2">
            <option selected>None</option>
            <option selected>English</option>
            <option selected>Spanish</option>
           </select>

<?php
$Student1Classes = array('1Class1', '1Class2');
$Student1Courses = implode(", ", $Student1Classes);
?>
</code>

If no option is chosen, this sends to email like this: Student1Classes: None, None

(Of course, the automatically generated letter that my php script (that does the mailing) calls for $Student1Classes, but shows:
 Array
 Array
- totally different issue, so probably not relevant here, but in case it was relevant I posted it here too)

I apologize that I'm not very clear, as I've been inundated in this since yesterday morning to no avail.  I hope that clears up (at least a little better) what I am needing here.  Thank you for all your help!

code_red
Ray PaseurCommented:
<?php // RAY_temp_code_red.php
error_reporting(E_ALL);
echo "<pre>";

// IF THE FORM HAS BEEN SUBMITTED
if (!empty($_POST))
{
    // ACTIVATE THIS TO SHOW THE POSTED DATA
    // var_dump($_POST);

    // SANITIZE AND PROCESS THE ARRAY
    $chosen = array();
    $found  = FALSE;
    foreach ($_POST['Student1Classes'] as $class)
    {
        $chosen[] = $class;
        if ($class != 'None') $found = TRUE;
    }

    // IF ANYTHING WAS FOUND
    if ($found)
    {
        $classes = implode(',', $chosen);
        echo PHP_EOL;
        echo "YOU CHOSE: $classes";
    }
    else
    {
        echo PHP_EOL;
        echo "YOU CHOSE NOTHING";
    }
}

// USE HEREDOC NOTATION TO CREATE THE HTML FORM
$form = <<<FORM
<form method="post">
<select name="Student1Classes[]" id="1Class1">
<option value="None" selected>None</option>
<option value="Math">Math</option>
<option value="Science">Science</option>
</select>

<select name="Student1Classes[]" id="1Class2">
<option value="None" selected>None</option>
<option value="EN">English</option>
<option value="ES">Spanish</option>
</select>

<input type="submit" />
</form>
FORM;

echo $form;

Open in new window


In case you're new to PHP and MySQL this little book will be very helpful.  It is well organized for structured learning and very readable. Great examples, and a code library that you can copy and modify for your own use.
http://www.sitepoint.com/books/phpmysql4/

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
code_redAuthor Commented:
Thank you, Ray. I was still unable to get this to do anything different than what my original code was already doing.  It's probably me.  I've been working with PHP for several years, but I don't work with it often.  Thank you for the help and the link!  I'll award the points since you did give me very usable code, just not exactly what I needed (again, I'm sure it's me).  Although I'm sure it will be very useful for others.

code_red
Ray PaseurCommented:
Thanks for the points.  I can't really write your code for you, but hopefully the design pattern is easy enough to follow that you can see the moving parts and implement them in your application.  Best of luck with it, ~Ray
code_redAuthor Commented:
Ok, here is what I ended up doing...
On the formmail page (after form is submitted - this page holds the code sends the email):
<?php
$Student1Classes = implode(", ", $_POST["Student1Classes"]);
if($Student1Classes=="None, None")
{
$Student1Classes = NULL;
}
?>

What this does is still send the email with the form results to me as "None, None", BUT the letter that is also generated to go to the user (on the same page as the formmail code - and the above code), DOES in fact go NULL.  This will definitely suffice.  

Thank you for leading me in the right direction.
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
PHP

From novice to tech pro — start learning today.