无法弄清楚这一点

时间:2013-06-06 15:25:26

标签: php sql

好的,我们走了..

让我们说$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));

这是我正在尝试实施的内容:custom php forum - showing new/unread posts

3 个答案:

答案 0 :(得分: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

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

考虑这个流程:

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

答案 1 :(得分:0)

您的“将他添加到列表”代码会使用新用户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.

那就是说,你所做的是一个非常糟糕的主意。不过,如果不了解你的项目,我真的不能告诉你什么是个好主意。

答案 2 :(得分: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;
}