Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

What's being returned with this function?

Posted on 2016-09-01
6
Medium Priority
?
43 Views
Last Modified: 2016-09-01
Here's the code:

class AccountTimelines extends CI_Model {

  function getAccountTimeline($accountid){
    $sql = "select * from
    view_AccountTimeline where accountid='$accountid'
    order by eventdate asc";
	//echo $sql;
    $set = pconnectdb($sql, DEFAULT_DB, true);
	//echo $set;
    $runningbal = 0;
    foreach($set as &$item){
      if($item['eventtype'] == 'transaction'){
        $runningbal += pfmoney::toCents($item['amount']);
        $item['runningbal'] = pfmoney::fromCents($runningbal);
      }
    }
	//echo $set;
    return $set;
  }
}

Open in new window


When I uncomment "echo $set," I get "Array," which is what I expect. I can't tell what that "foreach" is doing and if it's part of what's being "returned."

What do you think?
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
6 Comments
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 400 total points
ID: 41779977
I think that pconnectdb() be a cutom function which establishes a db connection, execute a query and return a result set as array.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41779978
You should find it somewhere in your project...
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41779995
It looks like the function is adding up some figures and injecting them into the database query results set, then returning the results set in the form of an array.

PHP echo is the wrong tool.  You'll get better information with var_dump()

When you echo an array, PHP unhelpfully returns the word "Array."  So use var_dump() to see what is in the array, maybe to compare before-and-after snapshots of the array.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 34

Accepted Solution

by:
Slick812 earned 1000 total points
ID: 41780092
OK, , The MAIN thing to notice, to understand WHY this getAccountTimeline( ) function was used to extend the CI_Model Class, is the PHP reference character  as
     &

in this line -
     foreach($set as &$item){

When that  & is added before the   $item    , THEN you can alter, change and add-to each ROW in the  $set  Array  , , , which is all of the ROWS from the DB SELECT in the $set Array.
There is NO DB TABLE column name of  'runningbal' , so the PHP   foreach( )   will get the ROW column value for  $item['amount'] and then convert it to a money amount with  pfmoney::toCents( ) static function, , AND progressively use math addition to get a running balance with -
     $runningbal += pfmoney::toCents($item['amount']);

and then the column name of  'runningbal'  is placed in every ROW in $set with this line -
      $item['runningbal'] = pfmoney::fromCents($runningbal);
It CHANGES the $set array rows , because the & was added to the $item on line -
   foreach($set as &$item){
so this function returns the SQL SELECT ROWS array as $set, BUT all of the rows now have the column name of  'runningbal', which they did not before the foreeach( ) added them.
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 600 total points
ID: 41780172
@slick812 makes a good point about the use of the ampersand in &$item.  Here's the full explanation:
https://www.experts-exchange.com/articles/12310/PHP-Variables-and-References.html

Also, please see this note.  A modern implementation of this function would probably use object-oriented code to do all the work with a minimum of PHP code.
http://php.net/manual/en/mysqli-result.fetch-object.php#92784
0
 

Author Comment

by:brucegust
ID: 41780804
Got it!

Marco, you were right. I did find the pconnectdb function and it had some notes documented at the top of the file that stated that all SELECT statements were being returned, by default, as an associative, multi-dimensional array. So, there's that piece.

Ray, I created a "sandbox" and used the var_dump to confirm some things and both you and Slick were right. I appreciate the commentary and references to other articles. I was able to figure out both the WHAT and the WHY.

Thank you!
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

610 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