Solved

What's being returned with this function?

Posted on 2016-09-01
6
26 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
6 Comments
 
LVL 30

Assisted Solution

by:Marco Gasi
Marco Gasi earned 100 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 30

Expert Comment

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

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 33

Accepted Solution

by:
Slick812 earned 250 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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 150 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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now