What's being returned with this function?

Posted on 2016-09-01
Medium Priority
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?
Question by:brucegust
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
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.
LVL 31

Expert Comment

by:Marco Gasi
ID: 41779978
You should find it somewhere in your project...
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.
Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

LVL 34

Accepted Solution

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.
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:

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.

Author Comment

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!

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to count occurrences of each item in an array.
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 …
Suggested Courses

762 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