[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

What does this mean?

Posted on 2016-10-06
6
Medium Priority
?
58 Views
Last Modified: 2016-10-06
I've got a SELECT statement configured in PHP, the last part of which looks like this:

 and cg.id in (select chargegroupid from txn where accountid = $statement[AccountID]) "
            . (
                $filterDate ?
                    "
                    and cg.id not in (
                        select chargegroupid from txn where accountid = $statement[AccountID] group by chargegroupid having max(posted) > dbo.ufn_lastpaperstatementbefore($statement[AccountID], cast('2050-01-01' as date))
                    )"
                    :
                    ""
            ) . "
            group by t1.accountid, t1.chargegroupid, t1.encountercode "
            . (
                is_int($ageOutZeroAfter)?
                    " having datediff(d, max(t1.created), getDate()) <= $ageOutZeroAfter or sum(amount) <> 0 "
                    :
                    ""
            ) . "
order by dos";

Open in new window


When you print that portion of the sql out in MSSQL Studio, it looks like this:

cg.id in (select chargegroupid from txn where accountid = 12159877) group by t1.accountid, t1.chargegroupid, t1.encountercode having datediff(d, max(t1.created), getDate()) <= 45 or sum(amount) <> 0 order by dos

I didn't want to thicken the plot unnecessarily, but for the sake of ensuring you've got a complete picture of what's going on, here's the whole function:

protected static function getOldTransactions(
        $statement,
        $showPhysician = true,
        $filterDate = true,
        $ageOutZeroAfter = false
    ){
        /**
        Get the Old Transactions
         **/
        //krumo('oldtxn');
        $sql = "
            select
                t1.accountid,
                t1.chargegroupid,
                t1.encountercode,
                /* Max automatically disregards nulls */" . (
                    $showPhysician ?
                    "
                    coalesce(max(physician), '')
                    "
                    :
                    ("'" .
                    str_replace("'", "''", $statement["PracticeName"])
                    . "'")
                ) . " as provider,
                sum(case when t1.type = 'c' then amount else 0 end) as total_charges,
                sum(case when t1.type = 'a' then amount else 0 end) as total_adjustments,
                sum(case when t1.type = 'p' then amount else 0 end) as total_payments,
                sum(amount) as left_to_pay,
                /* Charge description */
                /* blank for now */
                '' as description,
                 cast(min(case when t1.type = 'c' then dos else null end) as date) as dos

            from txn t1
                left join chargegroup cg on t1.chargegroupid = cg.id
                /* Charge description join */
                left join (
                    select
                        max(case when (txn.type = 'C') then amount else null end ) as max_charge_amount,
                        accountid,
                        chargegroupid
                    from txn
                    group by accountid, chargegroupid
                ) t2 on t2.accountid = t1.accountid and t2.chargegroupid = t1.chargegroupid
                where t1.accountid = $statement[AccountID]
                and reversedref is null
                and (cg.status1 not in('rt','hd','rp','fc') or cg.status1 is null)
                and cg.id in (select chargegroupid from txn where accountid = $statement[AccountID]) "
            . (
                $filterDate ?
                    "
                    and cg.id not in (
                        select chargegroupid from txn where accountid = $statement[AccountID] group by chargegroupid having max(posted) > dbo.ufn_lastpaperstatementbefore($statement[AccountID], cast('2050-01-01' as date))
                    )"
                    :
                    ""
            ) . "
            group by t1.accountid, t1.chargegroupid, t1.encountercode "
            . (
                is_int($ageOutZeroAfter)?
                    " having datediff(d, max(t1.created), getDate()) <= $ageOutZeroAfter or sum(amount) <> 0 "
                    :
                    ""
            ) . "

        order by dos";
		echo $sql;
        return StatementImage::runQuery($sql);
    }

}

Open in new window


so $filterDate is true and  $ageOutZeroAfter = false

I'm not certain if the "?" represents the equivalent to an IF clause or a CASE dynamic. But what would the purpose be if the properties are fixed?

Bottom line: I don't understand the purpose of the "?" and the overall flow.

Thoughts?
0
Comment
Question by:brucegust
[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
  • 3
  • 2
6 Comments
 
LVL 44

Assisted Solution

by:zephyr_hex (Megan)
zephyr_hex (Megan) earned 1000 total points
ID: 41832088
oh lord, what horrible coding style!  no wonder it's cryptic.  That is a PHP condition wrapped in the middle of a string concatenation.

To pull out the condition:

$filterDate ? "then return this string" : ""

Open in new window


which reads as:  If $filterDate then return t his string otherwise return an empty string.

$filterDate ?  "and cg.id not in (select ...blah blah blah" : ""

Open in new window

0
 
LVL 59

Accepted Solution

by:
Julian Hansen earned 1000 total points
ID: 41832119
The ? : is called a ternary operator - it is a shorthand if statement

if ($expr) {
  $x = 1;
}
else {
  $x = 2;
}

Open in new window

In a ternary expression looks like this
$x = $expr ? 1 : 2;

Open in new window

They are useful sometimes but if used incorrectly can make your code a nightmare.

I would never use it like it has been done in the query - the potential for things to go wrong is immense and it makes for spaghetti code

Preference for strings is always HEREDOC
$id = $authenticated ? 'admin' : 'public';
$query = <<< QUERY
    SELECT * FROM {$table} WHERE type='{$type}'
QUERY;

Open in new window

Any variables that require complex expressions are set up front and then used in the HEREDOC.
0
 

Author Comment

by:brucegust
ID: 41832205
Thank you!

So, basically, since $filterDate is always true, I can expect...

   and cg.id not in (
                        select chargegroupid from txn where accountid = $statement[AccountID] group by chargegroupid having max(posted) > dbo.ufn_lastpaperstatementbefore($statement[AccountID], cast('2050-01-01' as date))
                    )

Open in new window


...to always run. I'll never have an empty string.

Got it!

And Julian, I went out and looked up "ternary." I see how it's an appropriate label in that you're looking at "three" elements as part of building the IF dynamic. Makes sense, although I can see how there's a lot of room for error.

I was able to go back into the code and see how every time this method is called, the $ageOutZeroAfter value is passed into the method as the number 45, which I'm assuming overrides the property as it's defined by default as "false."

Correct?

Thank you, gentlemen!
0
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!

 
LVL 44

Expert Comment

by:zephyr_hex (Megan)
ID: 41832312
Yes, if $ageOutZeroAfter is passed in, it will override the default 'false'.

That being said, it's against generally accepted good coding practices to allow a variable to change data types.  In this case, $ageOutZeroAfter could be both a boolean and a number.  PHP is not a strongly typed language, so it allows variables to change data types.  But just because it allows it doesn't mean it should be done.  It's a potential source for bugs.  So it should be avoided.  Booleans variables should only be assigned true / false.  Numeric variables should only be assigned a number.
0
 

Author Comment

by:brucegust
ID: 41832399
It's funny that you say that because this is the way the method is called earlier in the page:

$number_of_days_to_keep_zero_balance_encounters_on_the_statement = 45;
$st['oldtxns'] = StatementImage::getOldTransactions(
      $st,
      $st['showphysician'] == '1',
      $st['StatementRollUp'] == '0',
      $number_of_days_to_keep_zero_balance_encounters_on_the_statement
);

Here's the way the method appears on the page:

    protected static function getOldTransactions(
        $statement,
        $showPhysician = true,
        $filterDate = true,
        $ageOutZeroAfter = false
    ){

So, if I understand you correctly and I'm reading this right, $filterDate is being set by $st['StatementRollUp'] which is 0. That's going to resonate as "false-y," will it not? That means that part of the method will never run, thus making a large part of this SELECT irrelevant.

There you go...

Thanks!

BTW: I would love y'all to take a look at my code samples and give me some input: https://www.experts-exchange.com/questions/28974267/My-Code-Samples-What-Do-You-Think.html
0
 
LVL 44

Expert Comment

by:zephyr_hex (Megan)
ID: 41832433
Yes, the string '0' will be interpreted as false.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

656 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