What's being returned with this function?

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?
brucegustPHP DeveloperAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

Slick812Connect With a Mentor Commented:
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.
Marco GasiConnect With a Mentor FreelancerCommented:
I think that pconnectdb() be a cutom function which establishes a db connection, execute a query and return a result set as array.
Marco GasiFreelancerCommented:
You should find it somewhere in your project...
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Ray PaseurCommented:
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.
Ray PaseurConnect With a Mentor Commented:
@slick812 makes a good point about the use of the ampersand in &$item.  Here's the full explanation:

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.
brucegustPHP DeveloperAuthor Commented:
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!
All Courses

From novice to tech pro — start learning today.