Solved

PHP if statement...

Posted on 2012-03-20
17
194 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
  • 8
  • 7
  • 2
17 Comments
 
LVL 108

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:MadShiva
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
 
LVL 108

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:MadShiva
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 108

Expert Comment

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

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 108

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 108

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 108

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 108

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now