Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2016-08-30
5
59 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 109

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 109

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

839 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