/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Best way to indentify if a user has read a reply on a thread

Hey everyone,

I have a small thread system I put together my self. I have user that log in and have a table that keeps information about the users (username, password, last time active, name etc.).

I would like to be able to have a bold text on all the threads that the user has not read the newest reply to. What is the best way to do this?

If some one could explain or point me in the right direction, it would be great!

Sincerely,
Magnus

to post a comment
PHP

15 Comments(s)

Copy linkTweet thisAlerts:
@kattenJan 14.2008 — I have done somthing like this to my problem

when writing this im att school so i do not know if this is a correct syntax.
<i>
</i>CREATE TABLE <span><code>forumreadlist</code></span> (
threadID int UNSIGNED NOT NULL,
userID int UNSIGNED NOT NULL,
lastread int(10) UNSIGNED NOT NULL,
FOREIGN KEY (<span><code>threadID</code></span>) REFERENCES <span><code>threads</code></span> (<span><code>id</code></span>) ON DELETE CASCADE,
FOREIGN KEY (<span><code>userID</code></span>) REFERENCES <span><code>users</code></span> (<span><code>id</code></span>) ON DELETE CASCADE,
KEY (threadID,userID)
);


Then when you selects the threads
<i>
</i>SELECT * FROM <span><code>threads</code></span> LEFT JOIN <span><code>forumreadlist</code></span> ON <span><code>userID</code></span> = &lt;userviewingforumID&gt;


[code=php]
// Connect to Database works perfectly with regular mysql functions
$sql = new PDO('mysql:host=localhost;dbname=forum','root','pass');

// userid
$user = 1;

$query = $sql->query('SELECT * FROM threads LEFT JOIN forumreadlist ON userID = '.(int) $user)->fetchAll();

foreach($query as $thread) {
// This only works if your thread table contains a lastpost_time preferably UNIX_TIMESTAMP
if($thread['lastread'] > $thread['lastpost_time']) {
// Make me bold
}
}
[/code]


Yours katten
Copy linkTweet thisAlerts:
@smickusJan 14.2008 — yeah what he said - but some of his code is wrong... so umm, just create a table which inserts the foreign key of the thread id, and the user id, and a 0 or 1 if it is read. then make a function to select the read column where the thread id is whatever the thread is, and the user id is set to whatever the user is... and then if it is 0 (unread) return true, else return false... and then do the conditional formatting based on the true/false return...
Copy linkTweet thisAlerts:
@kattenJan 14.2008 — @smickus: your not thinking very far are you... what i have written is correct and works perfectly. and also what your saying would not work in a larger scale.


For some strange reason i cannot edit my previs post and i forgot to add a bit of code.

In the page were you view the thread you must update the lastread timestamp
Copy linkTweet thisAlerts:
@smickusJan 14.2008 — Yes I'm saying some of your code is wrong in reference to:
[code=php].(int) $user)->fetchAll();[/code]
I totally don't get what that is...

You're also assuming he is using PHP 5.1 and the PDO.
Copy linkTweet thisAlerts:
@trymbillauthorJan 14.2008 — But, what if I'm using just normal mysql_query and mysql_fetch_array?

Right now, I have your suggested SQL table but I changed lastread into a datetime row because I've got my lastpost row as datetime, and I have this code:

<i>
</i>$forums_threads = mysql_query("select * from forums_threads LEFT JOIN forums_readlist ON userID = '$n[id]' order by lastpost desc LIMIT $start_from,10");

while($thread = mysql_fetch_array($forums_threads)) {
if($thread['lastread'] &gt; $thread['lastpost']) { echo "&lt;b&gt;"; }
echo $thread['title'];
if($thread['lastread'] &gt; $thread['lastpost']) { echo "&lt;/b&gt;"; }
}


And I've put this code on the "View thread" page:

<i>
</i>mysql_query("delete from forums_readlist where threadID='$thread[id]' and userID='$user[id]'");

$date = date('Y-m-d H:i:s');
mysql_query("insert into forums_readlist values('$thread[id]','$user[id]','$date')");


It doesn't seem to work. Everything seems to be right on the mysql side. When I open a thread there is a new row in the threads_readlist table but nothing seems to change on the page itself. I tried echo-ing $thread['lastread'] and it doesn't have any value, even on the threads where I know there is a value in threads_readlist ...

... do you know what's wrong here? I haven't been using LEFT JOIN a lot, so I don't fully understand the meaning of it =)

Thanks for the help!
Copy linkTweet thisAlerts:
@trymbillauthorJan 14.2008 — Another problem I've noticed ... when any user has viewed a thread, let's say it's thread with the ID 39, no other user gets submitted to the threads_readlist table on that thread ... why is that? :-/

Threads_readlist doesn't have any "Distinct" setting on it or anything like that.
Copy linkTweet thisAlerts:
@smickusJan 14.2008 — why are you deleting and then adding straight away after? Have you cleared your cache?
Copy linkTweet thisAlerts:
@trymbillauthorJan 14.2008 — I want to delete the information that is already in the database. I'm just doing this instead of doing

<i>
</i>$dataexists = mysql_query("select * from forums_reads where userID='$user[id]' and threadID='$thread[id]'");
$dataexists = mysql_num_rows($dataexists);
if($dataexists == '1') { mysql_query("update"); } else { mysql_query("insert"); }


If I'm doing it wrong, please say so. Are you talking about the browsers cache or the webs or SQL?

Thanks for the reply ...
Copy linkTweet thisAlerts:
@trymbillauthorJan 15.2008 — Can some one please explain this to me in a simple way? :-/
Copy linkTweet thisAlerts:
@trymbillauthorJan 15.2008 — Sorry for the triple post! :-)

I'm getting closer to the end of my quest here but I've got one problem left and that's probably just because I don't fully understand the JOIN LEFT clause.

I've got this mysql query:

select * from forums_threads as t1
left join forums_readlist as t2 on t2.userID='1'


This looks good on paper, but MySQL keeps giving me back each thread as many times as the user with ID='1' has read any thread. So it's like:

Each instance of userID='1' in forums_readlist multiplied by each row in forums_threads. So each thread is displayed as many times as "count(*) from forums_readlist where userID='1'" would equal.

Can some one explain to me why this is happening and what I'm doing wrong?
Copy linkTweet thisAlerts:
@kattenJan 15.2008 — Yes I'm saying some of your code is wrong in reference to:
[code=php].(int) $user)->fetchAll();[/code]
I totally don't get what that is...

You're also assuming he is using PHP 5.1 and the PDO.[/QUOTE]


(int) cast the varible after it to an interget its kinda like a fail safe so you cannot pass anything else then a integer into the query security meauser.

On thread: i think there is somthing wrong with the query but im at school and cannot help you with that now for a few more hours.

Also i looked at your query to insert, and you should use time() instead of what you are using now to get a more accurat result.
Copy linkTweet thisAlerts:
@kattenJan 15.2008 — i was thinking about somthing i forgot..

You should maybe change the user id of when the person is not logged in into session id/hash instead if you can view the forum without logging in of course.
Copy linkTweet thisAlerts:
@trymbillauthorJan 16.2008 — i was thinking about somthing i forgot..

You should maybe change the user id of when the person is not logged in into session id/hash instead if you can view the forum without logging in of course.[/QUOTE]


The forum is only for registered members, so that's not an issue.

I seem to be getting the same results every time and I'm not getting my head around my problem right now. I'm not sure if it's just me these days or what, but I would really like it if some one could explain to me what I'm doing wrong in my LEFT JOIN mysql query.

Sincerely,

Magnus
Copy linkTweet thisAlerts:
@trymbillauthorJan 28.2008 — Hello everyone again!

O.K., so I found out I had to change my "Storage Engine" from MyISAM to InnoDB for JOIN,LEFT JOIN and RIGHT JOIN to work. I've done that now.

I've also put a foreign key from threadID in my forums_readlist table over to ID in my forums_threads table.

Now, when I run this SQL in PHPmyadmin:

SELECT * FROM forums_threads LEFT JOIN forums_readlist ON userID='3'

With userID='3' being a user I know exists and has information in the forums_readlist table ... I just get each thread from 'forums_threads' repeated as many times as MySQL finds any information with userID='3' in the 'forums_readlist' table.

So I'm totally lost here! Don't I have to tell, in the SQL, what thread it's suppose to check?

Can some one please help me with this? I have to finish this as soon as possible ... :- /
Copy linkTweet thisAlerts:
@trymbillauthorJan 28.2008 — I found out what I was doing wrong.

I had to join both threaID and userID so my final SQL is as follows:

<i>
</i>select * from forums_threads
LEFT JOIN forums_readlist
ON threadID=id
AND userID='3'
×

Success!

Help @trymbill spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...