0

好的,我们开始吧..

假设$topic['is_new']由“7325823”组成,accID 是 63426,然后它会更新为 7325823|63426 但如果我再次重新加载页面,它会删除 7325823| 所以它只有 63426。我不想要那个。

怎么了?想不通

$accID = userid goes here;

$topic['is_new'] = "72482|81249|8124|42534|...and so on"; // user ids that i get from field in topics table
$list_of_ids = explode('|', $topic['is_new']); 

// lets see if the user has already been here
if (!in_array($accID, $list_of_ids)) {
$in = isset($topic['is_new']) && !in_array($accID, $list_of_ids) ?    $topic['is_new'].'|'.$accID : $topic['is_new'].'|'.$accID; 
} else {
// if he havent, add him to the list
$in = $accID;
}

// yes i know, PDO is better
mysqli_query("UPDATE topics
   SET num_views = num_views+1, is_new = '$in'
   WHERE id = $tid") or die(mysqli_error($link));

这就是我想要实现的:自定义 php 论坛 - 显示新的/未读的帖子

4

3 回答 3

0

您的“将他添加到列表”代码会用新用户 ID 覆盖列表,而不是附加到列表中。此外,代码的“查看用户是否已经到过这里”的形式为“如果 X 则 Y 否则 Y”。

尝试这样的事情:

$list_of_ids = $topic['is_new'] ? explode("|",$topic['is_new']) : array();
// above code ensures that we get an empty array instead of
//                              an array with "" when there are no IDS
if( !in_array($accID,$list_of_ids)) $list_of_ids[] = $accID;

$newIDstring = implode("|",$list_of_ids);
// do something with it.

也就是说,你正在做的是一个非常糟糕的主意。不过,如果不了解您的项目,我真的无法告诉您什么是好主意。

于 2013-06-06T15:32:06.817 回答
0

您的问题在您的 if 语句中:

// lets see if the user has already been here
if (!in_array($accID, $list_of_ids)) {
    //if the user is not in the array, they are added here
    //your if here was returnign the same thing either way, so simplify
    $in = $topic['is_new'].'|'.$accID; 
} else {
    //HERE is your problem ... 
    //if the user is found in the array, you set $in to just the account id and update the database ... 
    //I would think you would not want to do anything here ... 
    //so comment out the next line
    //$in = $accID;
}
于 2013-06-06T15:32:39.583 回答
0

这让我大吃一惊,也许是故意的。

$in = isset($topic['is_new']) && !in_array($accID, $list_of_ids) ?    $topic['is_new'].'|'.$accID : $topic['is_new'].'|'.$accID; 

结果是一样的:

? $topic['is_new'].'|'.$accID
: $topic['is_new'].'|'.$accID

这里的下一个(或主要)问题,你的 else 需要工作。

考虑这个流程:

$topic['is_new'] = '7325823';
$list_of_ids would contain 7325823
!in_array() so it appends it

刷新页面:

$topic['is_new'] = '7325823|63426';
$list_of_ids would contain 7325823, 63326
in_array() === true so $in = $accID

话题更新到63426?

于 2013-06-06T15:29:53.313 回答