codeigniter 从值结果中删除重复的ID

mkshixfv  于 4个月前  发布在  其他
关注(0)|答案(1)|浏览(46)

平台:codeigniter。
我在数据库中有一行评论,其中包含对广告的所有评论。评论行具有ID,广告ID,评论,对评论的响应:
x1c 0d1x的数据
我的问题是与重复的评论广告,所以如果用户把一个评论的广告,脚本将回复他与文本“感谢评论”。
正如你所看到的(随机用户可以评论我的广告)comenter_id = 112是评论不止一次,它会导致服务器过载。
我的代码是这样的:

$comment_list = $this->basic->get_all_comment_of_ads($adsid);
$commenter_count=isset($comment_list["commenter_info"]) ? count($comment_list["commenter_info"]) : 0 ;
if(isset($comment_list["commenter_info"]))
       {
          if(isset($comment_list["commenter_info"][$ad_account_id])) 
          {
            $commenter_count--; // if ad account is also commenter
            unset($comment_list["commenter_info"][$ad_account_id]);
          }
       }

字符串
所以在这个例子中,结果$commenter_count将是3(5 - 2重复)。
如何防止同一个comenter_id的评论被回复?
我试着去做:

$i=0;
$commenter_info=array();
foreach($comment_list as $info){
 $time=  isset($info['created_time'])?(array)$info['created_time']:"";
 $comment_info[$i]['created_time']=isset($time['date'])?$time['date']:"";
 $comment_info[$i]['commenter_name']=isset($info['from']['name'])? $info['from']['name']:"";
 $comment_info[$i]['commenter_id']=isset($info['from']['id'])?$info['from']['id']:"";
 $comment_info[$i]['message']=isset($info['commente_text'])?$info['message']:"";
 $comment_info[$i]['comment_id']=isset($info['id'])?$info['id']:"";
 /* Store Commenter info as unique */
 if(!isset($commenter_info[$comment_info[$i]['commenter_id']])){
 $commenter_info[$comment_info[$i]['commenter_id']]['name']=$comment_info[$i]['commenter_name'];
 $commenter_info[$comment_info[$i]['commenter_id']]['last_comment']=$comment_info[$i]['message'];
 $commenter_info[$comment_info[$i]['commenter_id']]['last_comment_id']=$comment_info[$i]['comment_id'];
 $commenter_info[$comment_info[$i]['commenter_id']]['last_comment_time']=$comment_info[$i]['created_time'];
    }
    $i++;
}


但它将返回到评论者计数(3)。
我停在这一点上,我不能得到的逻辑如何获得评论者_id和存储它,以检查是否评论者_id是重复的,并没有回答他后,第一次回答了。
我看了看大多数回复,但这对我的情况没有帮助。

iqjalb3h

iqjalb3h1#

我相信你想要的答案是commenter_id列上的UNIQUE索引(假设是MySQL)。假设你使用的是Codeigniter 4,你可以这样做:

$db = $this->db->table('comments');

$db->set([
'commenter_id'  => '',
'adsid'     => '',
'commente_text' => '',
'response_to_text'  => ''
]);

$db->ignore(true);
$db->insert();

字符串
注意你的ignore(true)是这里的关键。这在mysql中做了类似的事情:
INSERT IGNORE INTO comments ...
如果用户已经添加了注解(即具有相同DB键的元素),则基本上会忽略该行。但是,您应该考虑到用户可能希望update他们的注解!!在这种情况下,您可以调用update()函数并使用WHERE更新记录,而不是忽略它。

相关问题