-ADD_DESCR("member", __FILE__);
-
-$UID = $GLOBALS['userid'];
-$whereStatement = " WHERE visible='Y'";
-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 (isset($_POST['ok']))
- {
- $cnt = 0;
- foreach ($_POST['cat'] as $cat => $joined)
- {
- if ($joined == "N") $cnt++;
- }
- if (($cats - $cnt) < getConfig('least_cats'))
- {
- unset($_POST['ok']);
- $LEAST = true;
- }
- }
- if (isset($_POST['ok']))
- {
- foreach ($_POST['cat'] as $cat => $joined)
- {
- switch ($joined)
- {
- case "Y":
- $sql = "";
- $result_user = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_user_cats WHERE userid=%s AND cat_id=%s LIMIT 1",
- array($UID, bigintval($cat)), __FILE__, __LINE__);
-
- if (SQL_NUMROWS($result_user) == 0)
- {
- $sql = "INSERT INTO "._MYSQL_PREFIX."_user_cats (userid, cat_id) VALUES ('%s','%s')";
- }
- else
- {
- // Free memory
- SQL_FREERESULT($result_user);
- }
+addYouAreHereLink('member', __FILE__);
+
+$whereStatement = " WHERE `visible`='Y'";
+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 (!ifSqlHasZeroNumRows($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?
+ switch ($joined) {
+ case 'Y':
+ // Check if this category has an entry
+ if (countSumTotalData(getMemberId(), 'user_cats', 'id', 'userid', TRUE, sprintf(' AND `cat_id`=%s', bigintval($categoryId))) == 0) {
+ // No, so add it
+ addSql(sprintf('INSERT INTO `{?_MYSQL_PREFIX?}_user_cats` (`userid`, `cat_id`) VALUES (%s,%s)', getMemberId(), bigintval($categoryId)));
+ } // END - if