Forum Moderators: coopster

Message Too Old, No Replies

Looping through array with calls to many functions inside the loop

Looping through array with calls to many functions inside the loop

         

sharkgr8

7:03 am on Jul 9, 2008 (gmt 0)

10+ Year Member



I am working on a mutli-level marketing script. I need to calculate business produced between a given range of dates. The agents are like 2000+ stored in the database. I have to retrieve them all along with their business from the database and calculate their personal, team-based and average businesses. Calculating Average business itself requires iteration through all the downline agents alongwith calculating their individual business.

Now the problem occurs when I try to calculate average business of all the 2000+ agents at a time. The whole page crashes while retrieving them.

Here is the code:

function getAdminLeaders($parent,$startdate, $enddate, $paid,$teamtype,$type='sub', $av=0)
{

$arr = array();

$ids = //fetch id,name, title and parentID alongwith the business from the database which involves the join query between two tables(agents and business tables)

for($i=0,$j=sizeof($ids);$i<$j;$i++)
{
$arr1 = array();
$arr1['title'] = $ids[$i]['title'];
$arr1['name'] = $ids[$i]['name'];
$arr1['upline'] = getAgentNameTitle($id[$i]['parentID']);
$arr1['personal'] = getRawProduction($id[$i]['ID'], $startdate, $enddate, $paid, 'personal');
$arr1['gross'] = getRawProduction($id[$i]['ID'], $startdate, $enddate, $paid, $teamtype);

if($av==1)
$arr1 = getAV($id[$i]['ID'],$type,$paid,$startdate, $enddate,$arr1);
$arr[$id[$i]['ID']] = $arr1;
}
return $arr;
}

Functions used in above code explained:
1. getAgentNameTitle - retrieves name and designation of the parent agent.

2. getRawProduction - gets the personal business or business made by the downline agents of a particular agents. It involves a two join statements involving three tables alongwith UNION. Its the only way I could get all the downline agents according to their team types.

3. getAV - gets the average business of the individual which itself requires its own loop while getting the downline agents businesses. Actually it applies a special set of calculations which can't be modifiable.

Here you may see that while retrieving all the agents, I have to call 4 different functions for the individual agents. I think there may be some way to optimize it so that its become faster. Really going insane on optimizing it.

Please help!
Thanks in advance.

mrscruff

7:39 am on Jul 9, 2008 (gmt 0)

10+ Year Member



Just a quick idea:

foreach ($ids as $key => $data) {
$arr1 = array();
$arr1['title'] = $data['title'];
$arr1['name'] = $data['name'];
$arr1['upline'] = getAgentNameTitle($data['parentID']);
$arr1['personal'] = getRawProduction($data['ID'], $startdate, $enddate, $paid, 'personal');
$arr1['gross'] = getRawProduction($data['ID'], $startdate, $enddate, $paid, $teamtype);

if($av==1)
$arr1 = getAV($data['ID'],$type,$paid,$startdate, $enddate,$arr1);
$arr[$data['ID']] = $arr1;
}
return $arr;
}
}

Do you get a timeout error page when you try and run the page?

sharkgr8

3:53 pm on Jul 9, 2008 (gmt 0)

10+ Year Member



Thanks for replying mrscruff. I have already tried that option but it still gives a blank page on IE7. I have even tried to extend the max_execution_time to 90 secs but to no avail.