-if (IS_ADMIN()) $whereStatement = "";
-
-$result = SQL_QUERY("SELECT id, cat FROM `{!_MYSQL_PREFIX!}_cats`".$whereStatement." ORDER BY `sort`", __FILE__, __LINE__);
-$cats = SQL_NUMROWS($result);
-
-if ($cats > 0) {
- $LEAST = false;
- if (IS_FORM_SENT()) {
- $cnt = 0;
- foreach (REQUEST_POST('cat') as $cat => $joined) {
- if ($joined == "N") $cnt++;
- }
-
- if (($cats - $cnt) < getConfig('least_cats')) {
- REQUEST_UNSET_POST('ok');
- $LEAST = true;
- }
- }
-
- if (IS_FORM_SENT()) {
- foreach (REQUEST_POST('cat') as $cat => $joined) {
+if (isAdmin()) $whereStatement = '';
+
+// Get all categories
+$result = sqlQuery('SELECT `id`, `cat` FROM `{?_MYSQL_PREFIX?}_cats`' . $whereStatement . ' ORDER BY `sort` ASC', __FILE__, __LINE__);
+
+// Are there entries?
+if (!ifSqlHasZeroNums($result)) {
+ $LEAST = FALSE;
+ if (isFormSent()) {
+ $count = '0';
+ foreach (postRequestElement('cat') as $categoryId => $joined) {
+ if ($joined != 'Y') $count++;
+ } // END - foreach
+
+ if ((sqlNumRows($result) - $count) < getLeastCats()) {
+ unsetPostRequestElement('ok');
+ $LEAST = TRUE;
+ } // END - if
+ } // END - if
+
+ // Is the form sent?
+ if (isFormSent()) {
+ // Start counting all
+ $count = '0';
+
+ // Ini SQLs here
+ initSqls();
+
+ // Go through all entries
+ foreach (postRequestElement('cat') as $categoryId => $joined) {
+ // Has the user joined on this category?