Solved

PHP if statement...

Posted on 2012-03-20
17
199 Views
Last Modified: 2012-03-20
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
0
Comment
Question by:code_red
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
  • 2
17 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37742030
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?
0
 
LVL 10

Expert Comment

by:Tobias
ID: 37742034
Dear,

This code should do the work :


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

Open in new window


Regards
0
 

Author Comment

by:code_red
ID: 37742096
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
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37742173
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.
0
 
LVL 10

Expert Comment

by:Tobias
ID: 37742190
Dear,


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

}

Open in new window


Regards

//Corrected forgot to update in the array
0
 

Author Comment

by:code_red
ID: 37742248
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
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37742283
That's great.  I'll be back in few moments.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37742347
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

0
 

Author Comment

by:code_red
ID: 37742476
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
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37742493
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.
0
 

Author Comment

by:code_red
ID: 37742568
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?
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37742585
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).
0
 

Author Comment

by:code_red
ID: 37742731
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
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 350 total points
ID: 37744185
<?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/
0
 

Author Comment

by:code_red
ID: 37744352
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
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37744390
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
0
 

Author Comment

by:code_red
ID: 37744430
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.
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question