4images Forum & Community

4images Issues / Ausgaben => Discussion & Troubleshooting => Topic started by: alekinna on December 23, 2008, 04:11:53 AM

Title: Update comments count
Post by: alekinna on December 23, 2008, 04:11:53 AM
I do little mod that uses user_comments from USERS_TABLE and found that this value not changed if:

1. whole category was deleted
2. image was deleted in ACP  (member.php work fine)

To fix first problem I did
in the file admin/categories.php

find two times
Code: [Select]
$image_ids_sql .= (($image_ids_sql != "") ? ", " : "").$image_row['image_id'];
add below
Code: [Select]
$sql2 = "SELECT user_id
              FROM ".COMMENTS_TABLE."
              WHERE image_id = ".$image_row['image_id']." AND user_id <> ".GUEST;
      $result = $site_db->query($sql2);

      while ($row = $site_db->fetch_array($result)) {
        $sql3 = "UPDATE ".USERS_TABLE."
                SET ".get_user_table_field("", "user_comments")." = ".get_user_table_field("", "user_comments")." - 1
                WHERE ".get_user_table_field("", "user_id")." = ".$row['user_id'];
        $site_db->query($sql3);
      }


To fix second problem I did

in the file admin/images.php

find
Code: [Select]
    if (!empty($user_table_fields['user_comments'])) {
      $sql = "SELECT user_id
              FROM ".COMMENTS_TABLE."
              WHERE image_id = ".$image_row['image_id']." AND user_id <> ".GUEST;
      $result = $site_db->query($sql);

      while ($row = $site_db->fetch_array($result)) {
        $sql = "UPDATE ".USERS_TABLE."
                SET ".get_user_table_field("", "user_comments")." = ".get_user_table_field("", "user_comments")." - 1
                WHERE ".get_user_table_field("", "user_id")." = ".$row['user_id'];
        $site_db->query($sql);
      }
    }

replace with
Code: [Select]
//    if (!empty($user_table_fields['user_comments'])) {
      $sql = "SELECT user_id
              FROM ".COMMENTS_TABLE."
              WHERE image_id = ".$image_row['image_id']." AND user_id <> ".GUEST;
      $result = $site_db->query($sql);

      while ($row = $site_db->fetch_array($result)) {
        $sql = "UPDATE ".USERS_TABLE."
                SET ".get_user_table_field("", "user_comments")." = ".get_user_table_field("", "user_comments")." - 1
                WHERE ".get_user_table_field("", "user_id")." = ".$row['user_id'];
        $site_db->query($sql);
      }
//    }


Can someone just check my solution and tell me if I did right?
Title: Re: Update comments count
Post by: V@no on December 23, 2008, 07:12:51 AM
Thank you for reporting this bug and providing the fix.

I've posted it in bugs (http://www.4homepages.de/forum/index.php?topic=23644.0) report section.

The actual fix is different then you've provided.