We help IT Professionals succeed at work.
Get Started

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

Bruce Gust
Bruce Gust asked
on
178 Views
Last Modified: 2016-08-30
Here's the data as it's coming into the for loop:

sample data
Here's how it looks on the page:

sample view
Here's the for loop with my attempts to break it down line by line. Where am I right, where am I wrong?

Here's the 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


Here's the same code with some notes that explain it. Where am I nailing it, where am I wrong?

$data = pf::getAppData('/app/accounttimeline'); //line #1 - this variable references a function that returns an array. So, at this point $data is an array, with serveral indices that pertain to the "app" that the data is going to be displayed through. Here's that method:

  public static function getAppData($path){
    $where['value'] = $path;
    $at = AppTemplate::read("*",$where);
    $data['apptemplate'] = $at;
    if(isset($_REQUEST['a'])){
      $where['id'] = $_REQUEST['a'];
      $a = Apps::read("*", $where);
      $data['app'] = $a;
      $where['id'] = $a['filterid'];
      $f = AppFilters::read("value",$where);
      $data['filter'] = $f['value'];
    } else {
      $data['app'] = array();
      $data['app']['name'] = $data['apptemplate']['name'];
      $data['app']['description'] = $data['apptemplate']['description'];
      $data['app']['category'] = $data['apptemplate']['category'];
    }
    return array_merge($data, array("isPinned"=>Apps::isPinned($path)));
  }

Open in new window


$timeline = AccountTimelines::getAccountTimeline($accountid); // line #3 - the SELECT statement that produces the sample data I've got listed above

$data['timeline'] = array(); // enhancing the multi-dimensional "$data" array by adding another index called "timeline"
$txnGroup = array(); // instantiating a new array called $txnGroup

 for ($i=0; $i < count($timeline); $i++) {//setting up my for loop to iterate as many times as I have rows in my recordset
      $event = $timeline[$i];//not sure what this is. I'm thinking it's an index and I'm calling it "$event"

$event['demo'] = ($data['badge']['practiceid'] == '63'?$accountid : false);//I'm looking at a part of the array that was in place before we started the for loop and calling it "$event['demo'] and it's value is based on the practice id

Tell me where I'm right and where I'm missing it!

Thanks!
Comment
Watch Question
Most Valuable Expert 2011
Author of the Year 2014
Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
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