We help IT Professionals succeed at work.
Get Started

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

Bruce Gust
Bruce Gust asked
Last Modified: 2016-09-02
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'])),
			} 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'])),

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?
Watch Question
This problem has been solved!
Unlock 2 Answers and 7 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE