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 statsChcę 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.
<?php
function fbDeletePosts($isMod, $return) {
if (!FBTools::isModOrAdmin() && !$isMod) {
return;
}
$items = fbGetArrayInts("fbDelete");
$dellattach = 1;
// start iterating here
foreach ($items as $id => $value) {
$database->setQuery('SELECT id,catid,parent,thread,subject,userid FROM #__fb_messages WHERE id=' . $id);
if (!$database->query()) {
return -2;
}
$database->loadObject($mes);
$thread = $mes->thread;
if ($mes->parent == 0) {
// this is the forum topic; if removed, all children must be removed as well.
$database->setQuery('SELECT userid,id, catid FROM #__fb_messages WHERE thread=' . $id . ' OR id=' . $id);
foreach ($database->loadObjectList()as $line) {
$children[] = $line->id;
if ($line->userid > 0) {
$userids[] = $line->userid;
}
}
$children = implode(',', $children); }
else {
//this is not the forum topic, so delete it and promote the direct children one level up in the hierarchy
$database->setQuery('UPDATE #__fb_messages SET parent='' . $mes->parent . '' WHERE parent='' . $id . ''');
if (!$database->query()) {
return -1;
}
$children = $id;
$userids = $mes->userid > 0 ? $mes->userid : '';
}
//Delete the post (and it's children when it's the first post)
$database->setQuery('DELETE FROM #__fb_messages WHERE id=' . $id . ' OR thread=' . $id);
if (!$database->query()) {
return -2;
}
// now update stats
FBTools::decreaseCategoryStats($id, $mes->catid);
//Delete message text(s)
$database->setQuery('DELETE FROM #__fb_messages_text WHERE mesid IN (' . $children . ')');
if (!$database->query()) {
return -3;
}
//Update user post stats
if (count($userids) > 0
) {
$database->setQuery('UPDATE #__fb_users SET posts=posts-1 WHERE userid IN (' . $userids . ')');
if (!$database->query()) {
return -4;
}
}
//Delete (possible) ghost post
$database->setQuery('SELECT mesid FROM #__fb_messages_text WHERE message='catid=' . $mes->catid . '&id=' . $id . ''');
$int_ghost_id = $database->loadResult();
if ($int_ghost_id > 0) {
$database->setQuery('DELETE FROM #__fb_messages WHERE id=' . $int_ghost_id);
$database->query();
$database->setQuery('DELETE FROM #__fb_messages_text WHERE mesid=' . $int_ghost_id);
$database->query();
}
//Delete attachments
if ($dellattach) {
$database->setQuery('SELECT filelocation FROM #__fb_attachments WHERE mesid IN (' . $children . ')');
$fileList = $database->loadObjectList();
check_dberror("Unable to load attachments.");
if (count($fileList) > 0
) { foreach ($fileList as $fl) {
}
$database->setQuery('DELETE FROM #__fb_attachments WHERE mesid IN (' . $children . ')');
$database->query();
}
}
} //end foreach
FBTools::reCountBoards();
mosRedirect($return, _FB_BULKMSG_DELETED);
}
?>
Ten post edytował kwiecin 27.04.2009, 20:25:47