Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-08-30
5
Medium Priority
?
76 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 111

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
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

927 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