Jump to content

Two queries - advice needed


Goose87

Recommended Posts

Dear all,

 

I have the following two questions, something which has been bugging me for a while and I can't resolve them on my own.  Some outside input would be grately appreciated:

 

1) Showing users which posts are unread and new in forums.

 

I want to create an in-game forum (not a free external one).  Everything works except showing people which posts are new and they haven't read, i.e. when you all wanted to see this one, you could see it in orange, when someone else replies, the other people then see a new post.  So far I can easily show a new post with a time variable, simple, however... if someone reads a new post, I need it to go back to normal text, because they've read it.  The only method I can think of at the moment is to have a table storing which posts people have read, but I know that's the worst idea ever created by a code :D  Some advice on this would be happily accepted :D

 

2) This is a little bit harder to explain, but lets say I have 10 options.  I am then having a rand(1,100) created to find the value.  At the moment I have:

 

if($rand>0 and $rand<20){

  do something

} if($rand==99){

  do something } 

 

for 20% chance ofsomething happening and 1% chance of someting happening.  This is very annoying to code, and VERY hard to scale up, especially if you want to add more things to the equation. 

 

If someone could please give me some input into this problem, I would also be extremely grateful.

 

Many thanks in advance, I really appreciate the time you're spending to read this/help me.

 

Goose.

Link to comment
Share on other sites

for #1 just have a viewed db field and put the ids of the viewed posts into that field.. that or store it into a session.. and store the session id of the last session in the db so you can recall the session id of the user each time.. so the session file could be more of a storage container outside of the database

 

#2 you could simply loop from 1 - 100 and add 100 array entries with numeric values.. for each possible outcome.. then pull a random entry from that array.. so 20 entries for something.. 79 for nothing and 1 for something else.. but than again php random isn't completely random

Link to comment
Share on other sites

for #1

I understand that you can have a "viewed" in the database field, but there might be 1000 users that want to view it, how to I distinguish between the users?  That's why I was thinking I would need to create a table for "post id", "user id", "viewed", but I'm pretty sure that is very inefficient..

 

Regarding storing the session.. what do you mean by store the id of the session into the DB, store the values of that session?  You lost me a little bit there..

 

for #2

 

Ah ok, that's an interesting idea with the loop function and using an array.  That makes the scalability a little easier, but still slow.  Any other ideas for that one? (your idea is still a vaste improvement on mine! :D)

 

What makes you say php rand isn't completely random?  I know they say computers can't produce a truly "random" value, or is there something actually wrong with it?

 

Thanks for the response :)

Link to comment
Share on other sites

its what you thought about the rand, computers aren't able to produce a random number, they get a random number in those random functions based on ticks or something or w.e something that is constantly changing so you might end up leaning towards one side way more than the other but code wise you could probably pull off a percentage chance with my method.. and 100 loops happens in a milisecond lol it will not slow your script down any.

 

for #1.. store the session ID in the database under the user table.. like.. sess_id.. than whenever the user loses his session id cookie.. they'll have to re-login.. and when they do re-login you assign them with THAT session ID rather than a totally new one.. so your user will always get his session file back.. so you can store all your viewed post ids in his session.. under an array.. $_SESSION['viewed'] then when you're looping thru posts you just do

 

if (!$_SESSION['viewed'][post_ID]) displayAsNewPost();

Link to comment
Share on other sites

Thanks for the response.  I have a couple more questions on this matter though..

 

1) I have a session variable for the id of the user when they are logged into my website and a session for the username.  Should I create another session for the forums that they are viewing, then if they log out (or the session ends) I have to reallocate that same forum session back to that user, correct?

 

2) You said I have to store the ID of the session in the database.  I need some clarification on what exactly you mean by this.  I wasn't aware that sessions could have IDs and that you could store just an ID and it would keep the values you store in them.

 

3) My array knowledge is rather poor, so I will have to read up on it..  but I have to store something like $array = array ($post1, $post2, $post3, $post4);  correct?  Then I have to explode the array to get those values and then add to them when I want to add more posts that they have read?

 

4) You said "session id cookie".  Does that mean I need to store a cookie on their computer, and have a session related to that cookie??  I also don't use cookies, I just use sessions on my website.

Link to comment
Share on other sites

session_id.. each time you run session_start() if the user isn't passing in his session_id to php via GET, POST, COOKIE, it will generate a new session ID.. meanwhile you're gona rely on COOKIE and other REQUEST vars just the same. You save the session_id into the database.. then you call session_id in the login process when the user sends the correct credentials.. before you call session_start() you will set up session_id($mysqlResult['sess_id']); THEN you'd call session_start().. and then your user will always use the same session..

 

if you do not store the id in a db.. when the user cleans his cookies or the cookie dies.. you will end up giving him a completely new session file and then any viewed post ids you stored inside the session will be lost..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.