我需要为每个用户、每个主题创建一个唯一的匿名索引。每个帖子不是以这个索引开始的,但是一旦用户将一个帖子编辑成匿名的,他们就会被分配匿名id。这意味着主题的第二张海报并不总是“匿名2”。
我试图用下面的代码来解决这个问题,但是我得到了不同的海报和相同的匿名索引。
我想知道,有没有更好的方法来存储这些索引,并确保每个用户、每个主题没有重复的数字?我需要另做一张table吗?可能使用唯一约束。。?
我想我可以和你坐一张table
-主题id——一个非索引-
但从技术上讲,不会有重复的可能吗?再说一次,我已经将它存储在otherposts表中它自己的列anonymous\u index中了。我可以只搜索max(匿名索引),其中$topic\u id=some或类似的内容,而不必创建表。
我一直在尝试设计避免生成另一个表并依赖于最大anon索引或我所做的备用(count(distinct(poster\u id)),其中anonymous\u 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\u poster\u 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\u id---user\u id---是匿名的---匿名索引
1 12正确1
2 23正确2
3 12真1
4 23正确2
5 34真3
真正的结果(无论如何都是这样):
--post\u id---user\u id---是匿名的---匿名索引
1 12正确1
2 12正确2
3 23正确2
4 12真3
5 34真3
暂无答案!
目前还没有任何答案,快来回答吧!