Solved

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

Posted on 2016-08-30
5
48 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!
0
Comment
Question by:brucegust
  • 3
  • 2
5 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41776587
Is there any reason why you chose for() instead of foreach()?  If not, you might want to try foreach().  We could know better if we can see the contents of $data and $timeline from the first code snippet.
0
 

Author Comment

by:brucegust
ID: 41776627
You're talking about line 8, right Ray?

First off, I don't know. There may be some other things about this code that I'm not aware of that would dictate a "for" rather than a "foreach." But I'm thinking that the "for" was used because of wanting to include a specified condition that continues the loop until it's returned as "false," yes?

Here's the thing, though: The real collection of theoretical question marks begins here:

$event = $timeline[$i];//not sure what this is. I'm thinking it's an index and I'm calling it "$event"

Is that creating a situation where you're adding another "layer" to the array. Because of what's happening at this point, I now have an index called "$event." Is that right?

The code works, so I'm not having to troubleshoot anything, I just want to have a better idea of "why" it works.

Thanks!
0
 

Author Comment

by:brucegust
ID: 41776628
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41776676
for ($i=0; $i < count($timeline); $i++) {
	$event = $timeline[$i];

Open in new window

Here is how for() works.
http://php.net/manual/en/control-structures.for.php

This is saying to PHP, "Take the value of $i and use it as an array index into the $timeline array.  Get the contents of that array position and assign it to the $event variable.  Do this over and over again until the value of $i is equal to the number of elements in the $timeline array."
0
 

Author Closing Comment

by:brucegust
ID: 41776699
Perfect! Thanks!
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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
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