Solved

What's being returned with this function?

Posted on 2016-09-01
6
29 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 31

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 31

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
File submission to SQL Server 1 39
.php tree directory? 5 56
form button worked now it doesnt anymore 9 41
Testing a string condition in PHP 6 0
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

910 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

22 Experts available now in Live!

Get 1:1 Help Now