Re: Re: Rogue 'if - elseif' code

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



LinuxManMikeC wrote:
On Fri, May 22, 2009 at 2:50 PM, Nathan Rixham <nrixham@xxxxxxxxx> wrote:
Andre Dubuc wrote:
Hi,

I'm having problems with a chunk of 'rogue' code that does not perform as
expected (it does not pass the expected date, but an empty value). Most of
the time, it works - so I'm wondering whether it might be a browser issue.
(The latest failure occurred with Firfeox 3.0 browser on an NT 5.1 OS.) The
code is stored on an Unix server, running PHP 5.x.
I've isolated it down to multiple if - elseif statements that check long
dates for completeness, "day" "month" and "year". The code then applies it
results printing one of following: the full date - 22 May 2009, month-year -
May 2009, or just the year, 2009. My question is whether the ORDER of
checking is important, i.e, whether checking for the null sets BEFORE full
sets or AFTER.
I've added debugging code to be able to get the raw POST values so I can
manually enter it into the db, but I would really appreciate some help here.
The code is old -- I wrote it seven years ago, and had worked well until I
modified it, but I no longer have the original working code.
[The first conditional line of the code checks whether the user has
entered the birth date, then it checks for the death date. The $_SESSION
stuff is debugging code.]


<?php


       $yd = $_POST['death'];
       $yb =  $_POST['birth'];


       if ($_POST['bday'] == "Day" && $_POST['bmonth'] == "Month" &&
$_POST['birth'] == "Year") {


               if ($_POST['dday'] != "Day" && $_POST['dmonth'] != "Month"
&& $_POST['death'] != "Year") {

                       $_POST['rdod'] = ("{$_POST['dday']}
{$_POST['dmonth']} {$_POST['death']}");
                       $_SESSION['ALL1rdod'] = $_POST['rdod'];

               }


               elseif ($_POST['dday'] == "Day" && $_POST['dmonth'] ==
"Month" && $_POST['death'] != "Year") {

                       $_POST['rdod'] = $_POST['death'];
                       $_SESSION['YEAR1rdod'] = $_POST['rdod'];

               }


               elseif ($_POST['dday'] == "Day" && $_POST['dmonth'] !=
"Month" && $_POST['death'] != "Year") {

                       $_POST['rdod'] = ("{$_POST['dmonth']}
{$_POST['death']}");
                       $_SESSION['MONTHYEAR1rdod'] = $_POST['rdod'];

               }


               $_SESSION['Sdebugdod1'] = "{$_POST['dday']}
{$_POST['dmonth']} {$_POST['death']}";

       }


               else {


                       if ($yd > $yb) {


                               if ($_POST['bday'] != "Day" &&
$_POST['bmonth'] != "Month" && $_POST['birth'] != "Year") {

                                       $_POST['rdob'] = ("{$_POST['bday']}
{$_POST['bmonth']} {$_POST['birth']}");

                               }


                               elseif ($_POST['bday'] == "Day" &&
$_POST['bmonth'] == "Month" && $_POST['birth'] != "Year") {

                                       $_POST['rdob'] = $_POST['birth'];

                               }


                               elseif ($_POST['bday'] == "Day" &&
$_POST['bmonth'] != "Month" && $_POST['birth'] != "Year") {

                                       $_POST['rdob'] =
("{$_POST['bmonth']} {$_POST['birth']}");

                               }



                               if ($_POST['dday'] != "Day" &&
$_POST['dmonth'] != "Month" && $_POST['death'] != "Year") {

                                       $_POST['rdod'] = ("{$_POST['dday']}
{$_POST['dmonth']} {$_POST['death']}");
                                       $_SESSION['ALL2rdod'] =
$_POST['rdod'];

                               }


                               elseif ($_POST['dday'] == "Day" &&
$_POST['dmonth'] == "Month" && $_POST['death'] != "Year") {

                                       $_POST['rdod'] = $_POST['death'];
                                       $_SESSION['YEAR2rdod'] =
$_POST['rdod'];

                               }


                               elseif ($_POST['dday'] == "Day" &&
$_POST['dmonth'] != "Month" && $_POST['death'] != "Year") {

                                       $_POST['rdod'] =
("{$_POST['dmonth']} {$_POST['death']}");
                                       $_SESSION['MONTHYEAR2rdod'] =
$_POST['rdod'];

                               }


                               $_SESSION['Sdebugdod2'] = "{$_POST['dday']}
{$_POST['dmonth']} {$_POST['death']}";



                       }


?>

Any help or pointers would be greatly appreciated!

Tia,
Andre

makes no sense at all to me without some decent variable names "rdod"? -
would also need the form to be honest, and a description of the
functionality needed.

one thing i can say is that you have a closing bracket missing at the end
and your $_SESSION['Sdebugdod2'] appears to be in the wrong place
(to solve both these try sticking a } before $_SESSION['Sdebugdod2']

really though, change the names in the form to something more descriptive

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Yes, additional context would be great.  Also, it looks like Andre is
storing all his variables in $_POST, making it even harder to tell
what is from the HTML form and what is a programer's variable.  Andre,
IF I'm interpreting your explanation and code right, you are accepting
an entire date in a single field and it could be complete or partial.
If this is the case, you could much more easily validate the field
with a few regex patterns.  If you still need to work with the
individual components, either have separate date component fields in
your form, or use preg_split($regex,$date,PREG_SPLIT_DELIM_CAPTURE) to
parse the single input field for the date components.  For preg_split,
make your regex match valid date components and combined with the
DELIM_CAPTURE argument it will return an array of the date components.
 From there, do any further validation you need on the individual
components.  This would significantly shorten your code and make it
easier to read another 7 years from now.  And for goodness sake, I
hope you comment your code these days.


i.e. "that codes crap mate"

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux