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
Solved

What does this mean?

Posted on 2016-10-06
6
44 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
  • 3
  • 2
6 Comments
 
LVL 43

Assisted Solution

by:zephyr_hex (Megan)
zephyr_hex (Megan) earned 250 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 55

Accepted Solution

by:
Julian Hansen earned 250 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 43

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 43

Expert Comment

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

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 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 …

808 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