Witam, mam problem z poniższym kodem na moim forum.
Problem dotyczy aktualizowania ilości napisanych postów przez użytkownika.
Podczas usuwania całego tematu wraz z odpowiedziami po jednym poście jest zawsze odejmowane od każdego użytkownika,
który dał odpowiedź. Nie ważne czy 10 razy odpowiadał w tym temacie czy raz to zawsze będzie -1.
Ilość postów użytkownika przechowywana jest w tabeli fb_users pole posts.

Błąd występuje w kawałku kodu po komentarzu //Update user post stats
Chcę to zrobić tak, aby listować kolejno użytkowników może w pętli for i dla każdego zrobić zapytanie wybierające
wszystkie posty użytkownika z tematu, a potem do tablicy i zliczać w pętli.

Niestety w obiektowym php jestem laikiem a ten problem wydaje mi się że nie jest taki trudny do rozwiązania.
Proszę o pomoc.


  1. <?php
  2. function fbDeletePosts($isMod, $return) {
  3.        global $my, $database;
  4.  
  5.        if (!FBTools::isModOrAdmin() && !$isMod) {
  6.            return;
  7.            }
  8.  
  9.        $items = fbGetArrayInts("fbDelete");
  10.        $dellattach = 1;
  11.  
  12.        // start iterating here
  13.        foreach ($items as $id => $value) {
  14.            $database->setQuery('SELECT id,catid,parent,thread,subject,userid FROM #__fb_messages WHERE id=' . $id);
  15.  
  16.            if (!$database->query()) {
  17.                return -2;
  18.                }
  19.  
  20.            $database->loadObject($mes);
  21.            $thread = $mes->thread;
  22.  
  23.            if ($mes->parent == 0) {
  24.                // this is the forum topic; if removed, all children must be removed as well.
  25.                $children = array ();
  26.                $userids = array ();
  27.                $database->setQuery('SELECT userid,id, catid FROM #__fb_messages WHERE thread=' . $id . ' OR id=' . $id);
  28.  
  29.                foreach ($database->loadObjectList()as $line) {
  30.                    $children[] = $line->id;
  31.  
  32.                    if ($line->userid > 0) {
  33.                        $userids[] = $line->userid;
  34.                        }
  35.                    }
  36.  
  37.                $children = implode(',', $children);
  38.                $userids = implode(',', $userids);
  39.                }
  40.            else {
  41.                //this is not the forum topic, so delete it and promote the direct children one level up in the hierarchy
  42.                $database->setQuery('UPDATE #__fb_messages SET parent='' . $mes->parent . '' WHERE parent='' . $id . ''');
  43.  
  44.                if (!$database->query()) {
  45.                    return -1;
  46.                    }
  47.  
  48.                $children = $id;
  49.                $userids = $mes->userid > 0 ? $mes->userid : '';
  50.                }
  51.  
  52.            //Delete the post (and it's children when it's the first post)
  53.            $database->setQuery('DELETE FROM #__fb_messages WHERE id=' . $id . ' OR thread=' . $id);
  54.  
  55.            if (!$database->query()) {
  56.                return -2;
  57.                }
  58.  
  59.            // now update stats
  60.            FBTools::decreaseCategoryStats($id, $mes->catid);
  61.  
  62.            //Delete message text(s)
  63.            $database->setQuery('DELETE FROM #__fb_messages_text WHERE mesid IN (' . $children . ')');
  64.  
  65.            if (!$database->query()) {
  66.                return -3;
  67.                }
  68.  
  69.            //Update user post stats
  70.            if (count($userids) > 0) {
  71.                            
  72.                $database->setQuery('UPDATE #__fb_users SET posts=posts-1 WHERE userid IN (' . $userids . ')');
  73.  
  74.                if (!$database->query()) {
  75.                    return -4;
  76.                    }
  77.                }
  78.  
  79.            //Delete (possible) ghost post
  80.            $database->setQuery('SELECT mesid FROM #__fb_messages_text WHERE message='catid=' . $mes->catid . '&id=' . $id . ''');
  81.            $int_ghost_id = $database->loadResult();
  82.  
  83.            if ($int_ghost_id > 0) {
  84.                $database->setQuery('DELETE FROM #__fb_messages WHERE id=' . $int_ghost_id);
  85.                $database->query();
  86.                $database->setQuery('DELETE FROM #__fb_messages_text WHERE mesid=' . $int_ghost_id);
  87.                $database->query();
  88.                }
  89.  
  90.            //Delete attachments
  91.            if ($dellattach) {
  92.                $database->setQuery('SELECT filelocation FROM #__fb_attachments WHERE mesid IN (' . $children . ')');
  93.                $fileList = $database->loadObjectList();
  94.                    check_dberror("Unable to load attachments.");
  95.  
  96.                if (count($fileList) > 0) {
  97.                    foreach ($fileList as $fl) {
  98.                        unlink ($fl->filelocation);
  99.                        }
  100.  
  101.                    $database->setQuery('DELETE FROM #__fb_attachments WHERE mesid IN (' . $children . ')');
  102.                    $database->query();
  103.                    }
  104.                }
  105.            } //end foreach
  106.            FBTools::reCountBoards();
  107.  
  108.            mosRedirect($return, _FB_BULKMSG_DELETED);
  109.        }
  110. ?>