Random Link

Discussion in 'HTML & Website Design' started by geekazoid, Sep 11, 2006.

  1. #1
    Im looking for a script which i can paste into like 300 pages so when someone clicks on my "random link" picture it will look through a text document or a php document through all 300 of my pages and randomly select one line to send the visitor to when the visitor clicks the "random link" picture ... anyone know anything about this ?
     
    geekazoid, Sep 11, 2006 IP
  2. edelman

    edelman Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    this is pretty easy with PHP, ASP.NET, or any backend programming. The concept is the same in all places, though. Create an array with all your sayings (or even place them in a database) and when the user clicks the link, you have a randomization function that will go through and choose a number from your array and spit back the corresponding data. In .NET i know you need to initiate the Randomize() function, and I'm pretty sure there's something equivalent in PHP (maybe rand(0,300) in php?)

    HTH
     
    edelman, Sep 11, 2006 IP
  3. Pat Gael

    Pat Gael Banned

    Messages:
    1,331
    Likes Received:
    68
    Best Answers:
    0
    Trophy Points:
    0
    #3
    create a file text called random.lst adding your pages this way:
    
    http://your_site.com/page1.htm
    http://your_site.com/page2.htm
    http://your_site.com/page3.htm      .... etc. etc.  
    
    Code (markup):
    now another file called random.php with this code:
    
    <?php
    $RANDOM_URL_FILE = "random.lst"; 
    srand((double)microtime()*1000000);
    if (file_exists($RANDOM_URL_FILE)) {
    $arry = file($RANDOM_URL_FILE);
    for($i = 0; $i < sizeof($arry) ; $i++) {
    if (preg_match("/http:/", $arry[$i]))
    $good_arry[$j++] = chop($arry[$i]);
    # PHP 4.0 arry_push ($good_arry, $arry[$i]);
    }
    if ($good_arry) {
    header("Location: ".$good_arry[rand(0, sizeof($good_arry) -1)]);
    } else {
    echo "missing data file";
    }
    } else {
    echo "error: can't open $RANDOM_URL_FILE file";
    }
    ?>
    
    Code (markup):
    upload both to your root directory and call them this way:

    
    <a href="random.php">random link</A>
    
    Code (markup):
     
    Pat Gael, Sep 11, 2006 IP
  4. geekazoid

    geekazoid Peon

    Messages:
    208
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    geekazoid, Sep 11, 2006 IP
  5. Pat Gael

    Pat Gael Banned

    Messages:
    1,331
    Likes Received:
    68
    Best Answers:
    0
    Trophy Points:
    0
    #5
    randomizing is working but seems the problems is with your pages scripting.

    are Leaderboard.html, Horizontal.html, et within the Funny-Pictures directory?

    if not give to your pages the full url to such files, or place random.php and random.lst within this directory to correct the problem.
     
    Pat Gael, Sep 11, 2006 IP
    dabontv likes this.
  6. dabontv

    dabontv Active Member

    Messages:
    277
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #6
    hey thanks for that code, I've been using java all this time and this will save me a lot of time when updating.

    Also is there a way to display a random image (with link). Right now I'm using a .js to do that and just run three in a row so it displays 3 diffrent images, I'm guessing a php file would make it a lot more efficient.
     
    dabontv, Sep 11, 2006 IP
  7. Pat Gael

    Pat Gael Banned

    Messages:
    1,331
    Likes Received:
    68
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Definitely PHP is best than javascript and this in the way to accomplish your wish:

    
    <?php
    $pics = "10"; // number of images in your directory
    $ext = "gif"; // whatever image extension
    
    mt_srand((double)microtime()*1000000);
    $PicRandom = mt_rand(0,$pics);
    if ($PicRandom == "0") {
        $PicRandom = "1";
    }
    echo "<a href=\"$PicRandom.$ext\"><img src=\"$PicRandom.$ext\" alt=\"Random Picture #$PicRandom\"></a>";
    
    ?>
    
    Code (markup):
    Remarks:
    • all images must have the same extension
    • all images must be renamed accordingly (1.gif, 2.gif, 3.gif, etc.)
    • if you have numerous images, save time downloading Total Commander

    Total Commander is an alternative to Windows Explorer, with dual panels and multi-rename tool offering different options just by pressing Ctrl+M, available at ghisler.com
     
    Pat Gael, Sep 11, 2006 IP
  8. Scriptona

    Scriptona Notable Member

    Messages:
    4,957
    Likes Received:
    265
    Best Answers:
    0
    Trophy Points:
    280
    #8
    useful stuff here. thanks BasedSolutions :v
     
    Scriptona, Sep 12, 2006 IP
  9. Pat Gael

    Pat Gael Banned

    Messages:
    1,331
    Likes Received:
    68
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Glad to hear these pieces of code are helping you.

    Btw, another must-have for quick image procesing is irfanview, useful when you a mix of jpg, gif, png, etc.

    Using its batch facility you can make them all the same format at once.
     
    Pat Gael, Sep 12, 2006 IP