Forum Moderators: coopster
That id = 1 queries the mysql and gets a random url out of all the urls under the id 1 . Now I want that random url to be selected in such a way that it is not repeated again when the url is clicked again.
How do I do it? Is it with the help of cookies?
Check for a cookie with the name for this purpose first. if found, use this as the visitor's "id." If not, set a new one.
- Collect a "list" of URL id's to pull from.
select id from url_list . . .
array_push($url_list,$id)....
- Get any already-viewed id's from the database for this user . . .
select url_id from sessionviews where user='$sessionid"....
array_push($user_urls,$id)....
(Note: I use url_id because this should be numeric, it would be quite a bit slower matching on a full url - text - value.)
- Before doing anything, check if the length of $user_urls == the length of $url_list. If they are the same, you've completed the loop. Delete all entries from the database matching this user id, unset $user_urls, and pick a new random ID. Enter the new id in the database, display the url.
- If the two arrays are NOT the same length, remove all the "viewed" id's in $user_urls from the $url_list array, which makes only un-viewed ones available in $url_list. Randomly pick one, enter it's id into the database, display the url.
Example 2:
- Just like above, get your available URL ID's into an array.
- Check the cookie, except in this case your cookie value should be a delimited list, adding ID's as they are viewed:
URLVIEWS=325343344356456544645,2,4,7,8;
Explode the cookie on the commas;
$user_urls = explode(',',$cookie);
- Shift the first item off the array. This is your user id.
$uid = array_shift($user_urls);
- What's left in $user_urls is the url_id's already viewed. The same rules apply: if the length of $user_urls == $url_list, you're done with the loop, unset it and pic a new random.
- If the two arrays are NOT the same length, remove all the $user_urls from $url_list, and pick a random from what's left in $url_list.
- In either case, re-set the cookie that includes the same user ID and new URL ID, display the URL.
The advantage to this way is there's no need for a database, but there are limitations to cookie lengths so if you have thousands of URL's it may fail.
Probably better ways to do it, but these will work.
$url_list = array();
while(...
You are resetting the value of the array with each iteration.