【问题标题】:Can't figure this out想不通
【发布时间】:2013-06-06 15:25:26
【问题描述】:

好的,我们开始吧..

假设$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

【问题讨论】:

  • 您是否要为每个用户和每个线程保存哪些用户查看了哪些线程?那是你正在处理的O(N^M) 空间,这只是自找麻烦......
  • 脚本底部有一些 SQL,实际运行该 SQL 的代码在哪里?此外,您的三元运算符(从 isset($topic... 开始对两种情况都做同样的事情,因此是多余的。
  • @Kolink,我有一个非常小的论坛,我发现了这个,它看起来像是一个很好的简单解决方案:stackoverflow.com/questions/6374952/…
  • 不管它有多小,总要做出可以毫无困难地放大的东西。不要犯我的错误!我的网站在第一天就有 140 名用户,但在四天内就崩溃了 - 我不得不将其重新离线几天才能使其正常工作,而我仍在修补它以在三年后扩大规模!
  • @Kolink 你可能是对的。这似乎很容易。但这可能是个坏主意

标签: php sql


【解决方案1】:

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

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

【讨论】:

  • 我会标记这个答案,因为它的工作原理和我想要的一样。但我可能会研究其他方式。谢谢!
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • $accID (account_id) 是什么。如果查看线程的用户不在 is_new 我想把他的 accID 放在那里
    • 再次阅读流程,它会被覆盖,删除 else 并将 sql 移动到 if (!in_array 因为如果它已经在那里,您不需要更新它,对吧?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多