function groupRows(&$sqlQuery, $useLimit = false) { debug_printb("[groupRows] Grouping rows...<br>"); global $g_sqlGroupingFuncs; $ar_limit = $sqlQuery->limit; $groupColumns = $sqlQuery->groupColumns; $groupColNrs = array(); // use column numbers (faster) for ($i = 0; $i < count($groupColumns); ++$i) { $groupColNrs[$i] = $this->findColNrByFullName($groupColumns[$i]); if ($groupColNrs[$i] == NOT_FOUND) { print_error_msg("Column '" . $groupColumns[$i] . "' not found!"); return false; } } // calc limit if (!$useLimit) { $limit = -1; } else { if (!isset($ar_limit[0]) && !isset($ar_limit[1])) { $limit = -1; } else { if (count($ar_limit) > 1) { $limit = $ar_limit[0] + $ar_limit[1]; } else { $limit = $ar_limit[0]; } } } $rs = new ResultSet(); $rs->copyColumData($this); $groupedRows = array(); $groupedValues = array(); $colNamesCount = count($this->colNames); $this->reset(); while (++$this->pos < count($this->rows)) { // generate key $currentValues = array(); foreach ($groupColNrs as $groupColNr) { array_push($currentValues, md5($this->rows[$this->pos]->fields[$groupColNr])); } $groupedRecsKey = join("-", $currentValues); for ($i = 0; $i < $colNamesCount; ++$i) { $groupedValues[$groupedRecsKey][$i][] = $this->rows[$this->pos]->fields[$i]; } // key doesn't exist ? add record an set key into array if (!array_key_exists($groupedRecsKey, $groupedRows)) { $groupedRows[$groupedRecsKey] = 1; $rs->append(false); $rs->rows[$rs->pos]->fields = $this->rows[$this->pos]->fields; $rs->rows[$rs->pos]->id = $this->rows[$this->pos]->id; } if ($limit != -1) { if (count($rs->rows) >= $limit) { break; } } } --$this->pos; if (TXTDBAPI_VERBOSE_DEBUG) { echo "<b>RS dump in groupRows():<br></b>"; $rs->dump(); } $groupFuncSrcColNr = -1; // the source column for the column with grouping functions // calculate the result of the functions for ($i = 0; $i < count($rs->colFuncs); ++$i) { if (in_array($rs->colFuncs[$i], $g_sqlGroupingFuncs)) { if (TXTDBAPI_DEBUG) { debug_print("Searching source for grouping function " . $rs->colFuncs[$i] . "("); if ($rs->colTables[$i]) { debug_print($rs->colTables[$i] . "."); } debug_print($rs->colNames[$i] . "): "); } if ($rs->colFuncs[$i] == "COUNT" && $rs->colNames[$i] == "*") { $groupFuncSrcColNr = 0; } else { $groupFuncSrcColNr = $this->findColNrByAttrs($rs->colNames[$i], $rs->colTables[$i], ""); } if ($groupFuncSrcColNr == NOT_FOUND) { print_error_msg("Column " . $rs->colNames[$i] . ", " . $rs->colTables[$i] . " not found!"); return null; } foreach ($groupedValues as $key => $value) { $groupedValues[$key][$i][0] = execGroupFunc($rs->colFuncs[$i], $groupedValues[$key][$groupFuncSrcColNr]); } } } // put the results back $rs->reset(); foreach ($groupedValues as $key => $value) { $rs->next(); for ($i = 0; $i < $colNamesCount; ++$i) { $rs->rows[$rs->pos]->fields[$i] = $groupedValues[$key][$i][0]; } } return $rs; }