How to randomize links without repeating them ?

Reply

Join Date: Jul 2006
Posts: 174
Reputation: tefflox is an unknown quantity at this point 
Solved Threads: 1
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

How to randomize links without repeating them ?

 
0
  #1
Dec 27th, 2007
Hello, my site sees a lot of visits to the first page after the "cover", but very few beyond that page. I want to randomize the first inward link, and keep track, so if the reader decides to click "next", it won't load the same page. How do I do this, an external php script? (I've not done one of those yet.)

Thanks, and Season's Greetings

Jess
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,746
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 331
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: How to randomize links without repeating them ?

 
0
  #2
Dec 27th, 2007
Put all the links in an array, use rand function to get a random link!
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 174
Reputation: tefflox is an unknown quantity at this point 
Solved Threads: 1
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

Re: How to randomize links without repeating them ?

 
0
  #3
Dec 28th, 2007
I get that, but I don't want any redundancy in the links. Your suggestion might offer the same page twice or more times in a row.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: How to randomize links without repeating them ?

 
0
  #4
Dec 28th, 2007
Originally Posted by tefflox View Post
I get that, but I don't want any redundancy in the links. Your suggestion might offer the same page twice or more times in a row.
You'll need to keep track of the visited links via the client session. You can add it directly to PHP's built in session, or save visited links in a db.

eg:

  1. // links array
  2. $links = array(...);
  3.  
  4. // get users visited links to an array
  5. $visited_links = explode('|', $_SESSION['visited_links']);
  6.  
  7. // remove visited links from links array
  8. foreach($visited_links as $visited_link) {
  9. unset($links[array_search($visited_link, $links)]);
  10. }
  11.  
  12. // get a random link from unvisited links
  13. $link = $links[rand(0, count($links)-1)];
  14.  
  15. // add the selected link to visited array
  16. $visited_links[] = $link;
  17.  
  18. // save visited links to user session as | separated string
  19. $_SESSION['visited_links'] = implode('|', $visited_links);
Last edited by digital-ether; Dec 28th, 2007 at 12:51 am.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,746
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 331
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: How to randomize links without repeating them ?

 
0
  #5
Dec 28th, 2007
Exactly !
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC