0

Ok so here we go..

Lets say $topic['is_new'] consists of "7325823" and accID is 63426 then it updates to 7325823|63426 but if I reload the page again it removes 7325823| so its only 63426. I don't want that.

Whats wrong? 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));

This is what I'm trying to implent: custom php forum - showing new/unread posts

Community
  • 1
  • 1
Johnny
  • 23
  • 2
  • 4
    Are you trying to save what user has viewed what thread, for every user and every thread? That's `O(N^M)` space you're dealing with, which is just asking for trouble... – Niet the Dark Absol Jun 06 '13 at 15:28
  • You've got some SQL at the bottom of the script, where's the code that actually runs that SQL? Also, your ternary operator (starting `isset($topic...` is doing the same thing for both cases, and so is redundant. – Pudge601 Jun 06 '13 at 15:30
  • @Kolink, well I have a very very small forum and I found this and it seamed like a good easy solution: http://stackoverflow.com/questions/6374952/custom-php-forum-showing-new-unread-posts?answertab=active#tab-top – Johnny Jun 06 '13 at 15:45
  • 1
    No matter how small it is, always make something that can scale up without trouble. Don't make my mistake! My site got 140 users on the first day, and crashed within four days - I had to take it back offline for a few days to make it work, and I'm still patching it to scale up three years later! – Niet the Dark Absol Jun 06 '13 at 15:46
  • @Kolink Your probably right. It just seemed so easy. But it's probably a bad idea – Johnny Jun 06 '13 at 15:57

3 Answers3

0

This jumped out at me, maybe it's intended.

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

The outcome is the same:

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

The next (or main) issue here, your else needs work.

Consider this flow:

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

Refresh page:

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

Topics are updated to 63426?

somedev
  • 1,053
  • 1
  • 9
  • 27
  • Whataver $accID (account_id) is. If the user viewing the thread isnt in is_new i want to put his accID in there – Johnny Jun 06 '13 at 15:47
  • Read the flow again, it gets overwritten, remove the else and move the sql into the if (!in_array as you don't need to update it if it's already in there, right? – somedev Jun 06 '13 at 15:52
0

Your "add him to the list" code overwrites the list with the new user ID, rather than appending to it. Additionally, your "see if user has already been here" side of the code is of the form "if X then Y else Y".

Try something like this:

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

That said, what you're doing is an extremely bad idea. I can't really tell you what would be a good idea without knowing more about your project, though.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • I gonna mark this answer because it works like I wanted it to. But I'm probably gonna look into some other way. Thanks! – Johnny Jun 06 '13 at 16:06
0

Your issue is in your if statement:

// 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;
}
keithhatfield
  • 3,273
  • 1
  • 17
  • 24