PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 2nd, 2000, 01:58 PM
CecilyW CecilyW is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 2 CecilyW User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to CecilyW
Hi all, I'm very *very* new to PHP and just trying to wrap my mind around it. Thanks in advance for helping this newbie.

Here's what I want to do. A friend of mine has a php script that prints 5 random links from an array. However, his array contains over 300 links, whereas I only have about 100. I've pored over every PHP website I can think of, and I haven't been able to find away to keep the script from repeating the links more than once in the list. Here's the portion of the script that prints the array:

$anURL;

for ($loop = 0; $loop < 5; $loop++)
{

$n1 = 0;
$n2 = sizeof($URLs) -1;
srand((double)microtime()*1000000);
$num = (rand($n1,$n2));
print("<b>+ </b><a href = "http://");
print($URLs[$num]);
print("</a><br>");
}

What would I need to change here?

Thanks in advance!

Reply With Quote
  #2  
Old June 3rd, 2000, 05:22 AM
Shiju Rajan's Avatar
Shiju Rajan Shiju Rajan is offline
.Net Developer
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2000
Location: London
Posts: 987 Shiju Rajan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 26 m 22 sec
Reputation Power: 14
Send a message via MSN to Shiju Rajan Send a message via Yahoo to Shiju Rajan

CecilyW,

i would suggest you a logic.if you feel that is cool then you can go for it.

see ,,first you should generate a random number.

let us say that is 20.

now open a flatfile ,just write this number in the first row.

next what you will have to do is just generate 4 more numbers from 20. might be 20|21|22|23|24

<<all this step should be in a single go.
And also it should not be in an append mode.
>>

then read this numbers from this flat file for the link display.and assign these values to each link.

1st link will be 20th.
2nd link will be 21st one
....
....
....


In next refresh the random number will be changing and also the follwing numbers will be changing.That time it might be a sequance like 89|90|91|92|93.

so it will be keep on changing.make sure that this number is not going beyond the total number of links(ie, if it is more than 100 it should start writing from 0).

eg: 98|99|0|1|2


i have had faced similar situation many times.but i have implemented this logic successfully.

But only one problem it is having is ,only one link will fetch randomly.rest of the links will be generate from that random number.

if we are checking the condition it may take some time to process this script .so i thought this may be the easy solution for this..


actually i am not getting any other logic now.

probably others can suggest you a wonderful logic for solving your problem.

i hope above thing is clear to you.If you have any problem,then simply post that here.

Good Luck!!!





------------------
SR -
shiju.dreamcenter.net

"The fear of the LORD is the beginning of knowledge..."

Reply With Quote
  #3  
Old June 3rd, 2000, 12:36 PM
rycamor rycamor is offline
Gödelian monster
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 1999
Location: Central Florida, USA
Posts: 2,306 rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 6 h 42 m 51 sec
Reputation Power: 60
Shiju Rajan has come up with a very clever way to deal with the problem, if you do not have access to a database. If you have access to MySQL or some other database, though, then you should just input your library of links into the database and let it handle the randomizing function, because it will never duplicate random output.

A MySQL query for this would look something like:
$query = "SELECT id,linkdescription,url,id*0+RAND() as random_link FROM links ORDER BY random_link LIMIT 5"

It's a small amount of code, and this is the kind of thing databases were made for.

The benefit of this is the database handles the limiting of the array, so PHP only receives an array of 5 links, thus being much more efficient on memory and processor usage. It's a question of using the best tool for the job, whenever possible.

Reply With Quote
  #4  
Old June 3rd, 2000, 01:54 PM
CecilyW CecilyW is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 2 CecilyW User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to CecilyW
I don't have access to a mySQL database, but thanks for the suggestion.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Random Links in an Array

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap