Solved

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

Posted on 2016-08-30
5
64 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
[X]
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
  • 3
  • 2
5 Comments
 
LVL 110

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 110

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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
Ahax pagination 9 47
Generate PDF from MySQL using PHP 3 53
Multi line FPDF footer: 3 25
Find RGB colors from a screen. 2 17
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

756 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