?
Solved

What does this mean?

Posted on 2016-10-06
6
Medium Priority
?
54 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 58

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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

765 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