我想不通


Can't figure this out

好了。

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

什么错了吗?Can 't figure it out

$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论坛-显示新的/未读的帖子

您的"add him to the list"代码使用新的用户ID覆盖列表,而不是附加到列表中。此外,你的"查看用户是否已经在这里"的代码的形式是"if X then Y else 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.

也就是说,你这样做是一个非常糟糕的主意。但是,在不了解你的项目的情况下,我真的无法告诉你什么是好主意。

您的问题在您的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;
}

这让我大吃一惊,也许这是有意为之。

$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?