<?php $path = MODX_CORE_PATH . 'components/athletes/'; $result = $modx->addPackage('athletes',$path . 'model/','cr_'); /// Grab required values in Runner table, by race. $query = $modx->newQuery('Runner'); $query->select(array( 'AutoId', 'RegId', 'FirstLastName', 'BibNumber' )); $query->sortby('FirstLastName','ASC'); $query->where(array( 'RegTypeDescription' => $race )); ///$query->limit(100); $athletes = $modx->getCollection('Runner', $query); /// Grab all values in Time table. It grabs about 10 columns, I could restrict this to the 4 or so I need to speed it up? $athleteTimes = $modx->getCollection('Time'); $athleteTimesCount = count($athleteTimes); /// Loop through all the athletes entered for the race foreach($athletes as $athlete) { /// Grab the RegId into a variable to pass as criteria for the next foreach $athleteRegId = $athlete->get('RegId'); /// Loop through all the times that have been recorded so far at each aid station. up to 4,500 records. foreach($athleteTimes as $athleteTime) { $athleteTimeRegId = $athleteTime->get('timeRunnerId'); /// If there is a match, add those values to the athlete object if ($athleteTimeRegId == $athleteRegId) { $aidId = $athleteTime->get('timeStationId'); $athlete->fromArray(array( 'aid' . $aidId . 'Time' => $athleteTime->get('timeRunnerSecReadable'), 'aid' . $aidId . 'Pace' => 'Pace '.$athleteTime->get('timePaceFromStart'), ),'',true); } } /// use a template to display results $fields = $athlete->toArray(); $output .= $modx->getChunk('LiveRaceRow', $fields); } return $output;
<tr> <td> ? </td> <td> [[+FirstLastName]] </td> <td> [[+BibNumber]] </td> <td> [[+aid1Time]]<br /> <span class="twitterInfo">[[+aid1Pace]] </span> </td> <td> [[+aid2Time]]<br /> <span class="twitterInfo">[[+aid2Pace]] </span> </td> <td> [[+aid3Time]]<br /> <span class="twitterInfo">[[+aid3Pace]] </span> </td> <td> [[+aid4Time]]<br /> <span class="twitterInfo">[[+aid4Pace]] </span> </td> <td> [[+aid5Time]]<br /> <span class="twitterInfo">[[+aid5Pace]] </span> </td> <td> [[+aid6Time]]<br /> <span class="twitterInfo">[[+aid6Pace]] </span> </td> <td> [[+aid7Time]]<br /> <span class="twitterInfo">[[+aid7Pace]] </span> </td> <td> [[+aid8Time]]<br /> <span class="twitterInfo">[[+aid8Pace]] </span> </td> <td> [[+aid9Time]]<br /> <span class="twitterInfo">[[+aid9Pace]] </span> </td> <td> [[+aid10Time]]<br /> <span class="twitterInfo">[[+aid10Pace]] </span> </td> <td> [[+aid11Time]]<br /> <span class="twitterInfo">[[+aid11Pace]] </span> </td> <td> [[+aid12Time]]<br /> <span class="twitterInfo">[[+aid12Pace]] </span> </td> </tr>
<?php foreach ($athleteTimes as $athleteTime) { $athleteTimeRegId = $athleteTime->get('timeRunnerId'); $aidId = $athleteTime->get('timeStationId'); $times[$athleteTimeRegId][$aidId] = $athleteTime->toArray(); }
<?php foreach ($times[$athleteRegId] as $aidId => $time) { $athlete->fromArray(array('aid' . $aidId . 'Time' => $time['timeRunnerSecReadable'], 'aid' . $aidId . 'Pace' => 'Pace ' . $time['timePaceFromStart'], ), '', true); }
$athleteTimes = $modx->getIterator('Runner');
$output .= $modx->getChunk('LiveRaceRow', $fields);
<pre>Array ( [AutoId] => 145 [RegId] => 30108165 [EventId] => [EventTitle] => [ClientEventId] => [GroupId] => [RegDate] => [CancelDate] => [StatusDescription] => [RegTypeDescription] => [first_Name] => [last_Name] => [FirstLastName] => Aaron Martin [Address_1] => [Address_2] => [City] => [CityRegion] => [postal_Code] => [Country] => [Phone] => [HomePhone] => [Photo] => [CellPhone] => [Email_Address] => [SecondaryEmail] => [DirectoryOptOut] => [DateOfBirth] => [Gender] => [Nationality] => [GroupName] => [BibNumber] => 0 [EmergencyContactName] => [EmergencyContactPhone] => [roomSharerID] => [TotalCharge] => [BalanceDue] => [AddBy] => [AddDate] => [ModBy] => [ModDate] => [ChooseYourCategory] => [TshirtSize] => [ClubName] => [MedicalIssues] => [MoreDetails] => [Fitness] => [PreviousRunningExperience] => [Shuttle] => [aid1Time] => 00:45:45 [aid1Pace] => Pace 06:55 [aid2Time] => 02:02:02 [aid2Pace] => Pace 09:18 [aid3Time] => 03:03:03 [aid3Pace] => Pace 07:39 [aid4Time] => 05:05:55 [aid4Pace] => Pace 09:37 [aid5Time] => 06:45:55 [aid5Pace] => Pace 09:26 [aid6Time] => 10:02:30 [aid6Pace] => Pace 11:58 [aid7Time] => 13:27:00 [aid7Pace] => Pace 14:05 [aid8Time] => 15:55:00 [aid8Pace] => Pace 13:56 [aid9Time] => 18:44:06 [aid9Pace] => Pace 14:42 [aid10Time] => 20:33:06 [aid10Pace] => Pace 14:08 [aid11Time] => 23:55:08 [aid11Pace] => Pace 15:18 [aid12Time] => 24:59:08 [aid12Pace] => Pace 14:54 ) </pre>
<pre>Array ( [AutoId] => 217 [RegId] => 33846707 [EventId] => [EventTitle] => [ClientEventId] => [GroupId] => [RegDate] => [CancelDate] => [StatusDescription] => [RegTypeDescription] => [first_Name] => [last_Name] => [FirstLastName] => Ruth Belsom [Address_1] => [Address_2] => [City] => [CityRegion] => [postal_Code] => [Country] => [Phone] => [HomePhone] => [Photo] => [CellPhone] => [Email_Address] => [SecondaryEmail] => [DirectoryOptOut] => [DateOfBirth] => [Gender] => [Nationality] => [GroupName] => [BibNumber] => 0 [EmergencyContactName] => [EmergencyContactPhone] => [roomSharerID] => [TotalCharge] => [BalanceDue] => [AddBy] => [AddDate] => [ModBy] => [ModDate] => [ChooseYourCategory] => [TshirtSize] => [ClubName] => [MedicalIssues] => [MoreDetails] => [Fitness] => [PreviousRunningExperience] => [Shuttle] => [aid1Time] => 01:00:00 [aid1Pace] => Pace 09:05 ) </pre>
$placeholders = '[+field1+],[+field2+],[+field3+],[+field4+],[+field5+],[+field6+]';//add here all your fields $placeholders = exlode(',',$placeholders); $chunk = $modx->getObject('modChunk', array('name' => 'LiveRaceRow')); $template = $chunk->getContent(); foreach ($athletes as $athlete) { /* * more code */ $fields = $athlete->toArray(); $output .= str_replace($placeholders, $fields, $template); }
<?php $path = MODX_CORE_PATH . 'components/athletes/'; $result = $modx->addPackage('athletes',$path . 'model/','cr_'); $query = $modx->newQuery('Runner'); $query->select(array( 'AutoId', 'RegId', 'FirstLastName', 'BibNumber' )); $query->where(array( 'RegTypeDescription' => $race )); ///$query->limit(100); $athletes = $modx->getCollection('Runner', $query); $athleteTimes = $modx->getCollection('Time'); $athleteTimesCount = count($athleteTimes); foreach($athletes as $athlete) { $athleteRegId = $athlete->get('RegId'); /// echo 'athleteRegId:' . $athleteRegId . '<br/>'; for ($i = 1; $i <= 14; $i++) { $athlete->fromArray(array( 'aid' . $i . 'Time' => '-', 'aid' . $i . 'Pace' => '-', ),'',true); } foreach($athleteTimes as $athleteTime) { $athleteTimeRegId = $athleteTime->get('timeRunnerId'); /// echo 'athleteTimeRegId:' . $athleteTimeRegId . '<br/>'; if ($athleteTimeRegId == $athleteRegId) { /// echo 'We have a match <br />'; /// echo $athleteTime->get('timeRunnerSecReadable') . '<br />'; /// echo $athleteTime->get('timeStationId') . '<br />'; /// echo $athleteTime->get('timePaceFromStart') . '<br />'; $aidId = $athleteTime->get('timeStationId'); $athlete->fromArray(array( 'aid' . $aidId . 'Time' => $athleteTime->get('timeRunnerSecReadable'), 'aid' . $aidId . 'Pace' => 'Pace '.$athleteTime->get('timePaceFromStart'), ),'',true); } } /// $output .= '<pre>' . print_r($athlete->toArray(),true) . '</pre>' . $allTimesRegId; $fields = $athlete->toArray(); ///print '<b>Before sorting</b>: <br> <pre>'; ///print_r($fields); ///print '</pre>'; $output .= $modx->getChunk('LiveRaceRow', $fields); ///$output .= $modx->getChunk('LiveRaceRow', $fields); } return $output;
for ($i = 1; $i <= 14; $i++) { $athlete->fromArray(array( 'aid' . $i . 'Time' => '-', 'aid' . $i . 'Pace' => '-', ),'',true); }