#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    2
    Rep Power
    0
    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!
  2. #2
  3. .Net Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    London
    Posts
    987
    Rep Power
    15

    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..."
  4. #3
  5. No Profile Picture
    Gödelian monster
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 1999
    Location
    Central Florida, USA
    Posts
    2,307
    Rep Power
    61
    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.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    2
    Rep Power
    0
    I don't have access to a mySQL database, but thanks for the suggestion.

Similar Threads

  1. Writing array of random numbers to a database
    By Beda in forum ASP Programming
    Replies: 2
    Last Post: February 2nd, 2004, 03:34 PM
  2. feedback - span links = yuck
    By shreela in forum Dev Shed Lounge
    Replies: 2
    Last Post: January 28th, 2004, 11:59 PM
  3. sort: find duplicate string elements in array
    By sciencelife in forum Java Help
    Replies: 3
    Last Post: January 27th, 2004, 01:29 PM
  4. C++ programming help.
    By Programming1 in forum C Programming
    Replies: 1
    Last Post: January 20th, 2004, 10:47 AM
  5. Array manipulation and removal of array values
    By timwi in forum Perl Programming
    Replies: 10
    Last Post: January 14th, 2004, 10:49 AM

IMN logo majestic logo threadwatch logo seochat tools logo