troubleshooting Question

How does this "for" loop break down (Part II)?

Avatar of Bruce Gust
Bruce GustFlag for United States of America asked on
PHP
7 Comments2 Solutions203 ViewsLast Modified:
This is "Part II" of a two part question. Part I is at https://www.experts-exchange.com/questions/28966558/How-does-this-for-loop-break-down-Part-I.html. I've got a couple of graphics that provide some background that you might want to refer to.

We're at line 11 of the following code:

$data = pf::getAppData('/app/accounttimeline');
$timeline = AccountTimelines::getAccountTimeline($accountid); //the SELECT statement that produces the sample data I've got listed above
$data['timeline'] = array();
$txnGroup = array();
 for ($i=0; $i < count($timeline); $i++) {
	$event = $timeline[$i];
	$event['demo'] = ($data['badge']['practiceid'] == '63'?$accountid : false);
	if ($event['eventtype']=='transaction') {
		$thisDate = Date('mdy', strToTime($event['eventdate']));
		$nextDate = (isset($timeline[$i+1])&&$timeline[$i+1]['eventtype']=='transaction')?Date('mdy', strToTime($timeline[$i+1]['eventdate'])):null;
		array_push($txnGroup, $event);
		if ($thisDate != $nextDate) {
			//add to data
			if (count($txnGroup)!=1) {
				array_push($data['timeline'], array(
					'eventdate'=>Date('m/d/Y', strToTime($event['eventdate'])),
					'eventtype'=>'transactions',
					'runningbal'=>$event['runningbal'],
					'transactions'=>$txnGroup
				));
			} else {
				array_push($data['timeline'], $event);
			}
			$txnGroup = array();
		}

Line 11 is crucial to the project that I'm working on. We've got a multi-dimensional array that's being enhanced with the data coming from a SELECT statement. We're setting up some additional indices to hold more data and we're a point now where we're checking the eventtype to see if it's a "transaction:"

if ($event['eventtype']=='transaction') {//if the eventtype is a "transaction," then...
		$thisDate = Date('mdy', strToTime($event['eventdate'])); //set up $thisDate as a strtotime variable
		$nextDate = (isset($timeline[$i+1])&&$timeline[$i+1]['eventtype']=='transaction')?Date('mdy', strToTime($timeline[$i+1]['eventdate'])):null;//[b]if the next eventtype in the recordset is also a "transaction," then...not sure what this is doing[/b]
		array_push($txnGroup, $event);// you're using array_push to add everything that's currently compiled in the $event index to the $txnGroup array
 if ($thisDate != $nextDate) {//[b]if $thisData is not equal to $nextDate, then you've got a potential group of transactions as opposed to something that's flying solo[/b]
                        //add to data
                        if (count($txnGroup)!=1) {//if the there's more than one transaction in the $txnGroup, then we're going to make the eventtype "transactions," rather than "transaction"
                            array_push($data['timeline'], array(//push all of the following into the $txnGroup array
                                'eventdate'=>Date('m/d/Y', strToTime($event['eventdate'])),
                                'eventtype'=>'transactions',
                                'runningbal'=>$event['runningbal'],
                                'transactions'=>$txnGroup
                            ));
                        }

This is the last part of the "for" loop, but it represents something that I'm suspecting, but I'm not certain about...

}else {//if the eventtype is not a "transaction," then simply push what's there into the $event
array_push($data['timeline'], $event);
 }
 $txnGroup = array();//I think what's happening here is that I'm resetting the $txnGroup array to "". Rather than it being populated with anything, I'm starting fresh with the next round of $I in my "for" loop

Am I close?
ASKER CERTIFIED SOLUTION
Member_2_248744

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros