October 4, 201015 yr (PHP newbie beware) I am working with 2 tables, Classes and Sessions. I have built a page to display a list of Sessions, showing their related class title. I am looking for a way to group the results based on Class title, showing the title and description only once and the available sessions below. Below is all the PHP code to what I have created so far. If you have any suggestions or can point me to an article that will help, it would be much appreciated. thanks grum <?php error_reporting(E_ALL); require_once('db.php'); $request = $fm->newFindCommand('php_Session'); $request->addFindCriterion('StartDate', '>= //' ); $request->addSortRule('php_Class::classTitle', 1, FILEMAKER_SORT_ASCEND); $result = $request->execute(); $records = $result->getRecords(); $html = ' Sessions Available'; $group = array(); $group['total_class'] = 0; foreach ($records as $record) { $html .= ''; $html .= ''. $record->getField ('StartDate'). ''; $html .= ''. $record->getField ('php_Class::__kp_Class'). ''; $html .= ''. $record->getField ('php_Class::classTitle'). ''; $html .= ''. $record->getField ('php_Class::classDescription'). ''; } ?> Edited October 4, 201015 yr by Guest
October 14, 201015 yr Your html is technically invalid on the grounds that you're using the same id multiple times but never mind that... Easiest way is the following (though there are a few other ways you could do it). $groups = array(); /*Collect records in to groups*/ foreach ($records as $record) { $groups[$record->getField('php_Class::classTitle')][] = $record; } /*Process Groups*/ foreach($groups as $groupname=>$recordsInGroup){ $firstRecInGroup = current($recordsInGroup); echo " ".$groupname.""; echo " ".$firstRecInGroup->getField('php_Class::classDescription').""; echo ""; foreach($recordsInGroup as $record){ echo $record->getField ('StartDate').''; } } Edited October 14, 201015 yr by Guest
Create an account or sign in to comment