我需要为每个用户,每个主题创建一个唯一的匿名索引。每个帖子都不以该索引开头,但是一旦用户将帖子编辑为匿名,就会为其分配匿名ID。这意味着该主题的第二个发布者并不总是“ Anonymous 2”。
我试图用下面的代码解决这个问题,但是我得到的海报带有相同的匿名索引。
我想知道,是否有更好的方法来存储这些索引并确保每个用户每个主题没有重复编号?我需要单独制作一张桌子吗?也许使用唯一约束。.
我以为我可以有一张桌子
-topic_id --- anon_index-
结构之类的东西,但是从技术上讲,不会有重复的可能性吗?再说一次,我已经将其存储在其他帖子表的其自己的列anonymous_index中。我可以搜索max(anonymous_index),其中$ topic_id = some#或类似的东西,而无需创建表。
我一直在尝试反对制作另一个表,而依赖于我这样做的最大匿名索引或替代项(count(distinct(poster_id)),其中anonymous_index> 0),但是显然这里的内容是不正确的。
我像这样调用函数以获取匿名索引:
$data = $event['data'];
$username = $event['post_author_name'];
// get checkbox value
$anonpost = $event['post_data']['is_checked'];
$post_mode = $event['mode'];
$data['is_anonymous'] = $anonpost;
$data['was_anonymous'] = ($post_mode == 'edit') ? $event['post_data']['is_anonymous'] : 0;
$data['anonymous_index'] = ($post_mode == 'edit') ? $event['post_data']['anonymous_index'] : 0;
// fixed to return 1 for new topics, and mean it this time... wouldn't work sometimes for some weird reason
$get_anon_index = function() use($data, $post_mode)
{
if($data['is_anonymous'])
if($data['anonymous_index'] > 0) return $data['anonymous_index'];
else return $this->helper->get_poster_index($data['topic_id'], $data['poster_id']);
// 0 is default case... is default case really necessary?
return (int) ($post_mode == 'post');
};
$data['anonymous_index'] = $get_anon_index();
这是我的帮助文件中执行sql工作的get_poster_index函数:
// get unique poster index for consistent distinct anonymous posters
public function get_poster_index($topic_id, $poster_id)
{
// have we already anonymously posted in this topic?
// 0.7.0 - redundancy added (AND anonymous_index > 0)
$anon_index_query = 'SELECT anonymous_index
FROM ' . POSTS_TABLE . "
WHERE topic_id = $topic_id
AND poster_id = $poster_id
AND anonymous_index > 0
ORDER BY post_time ASC LIMIT 1";
$result = array();
$result = $this->db->sql_query($anon_index_query);
// these two get index of this post in that list
$anonymous_index = (int) $this->db->sql_fetchfield('anonymous_index');
$poster_index = $anonymous_index;
$this->db->sql_freeresult($result);
unset($result);
// this only runs if we've never posted in this topic, having data from previous query...
if($poster_index == 0)
{
$anon_index_query = 'SELECT COUNT(DISTINCT(poster_id)) AS anon_index
FROM ' . POSTS_TABLE . "
WHERE (topic_id = $topic_id AND anonymous_index > 0)
OR (topic_id = $topic_id AND is_anonymous = 1)";
$result = array();
$result = $this->db->sql_query($anon_index_query);
$poster_index = ((int) $this->db->sql_fetchfield('anon_index')) + 1;
$this->db->sql_freeresult($result);
unset($result);
}
return (($poster_index == 0) ? 1 : $poster_index);
我已经对此进行了测试,效果很好。没有索引重复。尽管实际上,有时两个不同的用户在一个主题中匿名发布将具有相同的匿名索引。
预期:
-post_id---user_id---is_anonymous---匿名索引
1 12是1
2 23是2
3 12是1
4 23是2
5 34是3
真实结果(总之如此):
-post_id---user_id---is_anonymous---匿名索引
1 12是1
2 12是2
3 23是2
4 12是3
5 34是3