Avatar of Bruce Gust
Bruce Gust
Flag for United States of America

asked on 

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

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();
		}

Open in new window


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
                            ));
                        }

Open in new window


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

Open in new window


Am I close?
PHP

Avatar of undefined
Last Comment
Bruce Gust

8/22/2022 - Mon