Rotating Text Links

Discussion in 'Programming' started by amelen, Jan 29, 2008.

  1. #1
    amelen, Jan 29, 2008 IP
  2. krzyk

    krzyk Peon

    Messages:
    61
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Well it is easy:

    You need to rotate the links so you need a random number, in PHP one can use rand(0, 10) to give a random number from 0 to 10 inclusive if you have that than a simple case statement will do the trick.

    Simple PHP function that returns random URL, should be self explanatory.

    
    function getTextLink()
    {
       $linkNum = rand(0, 3);
    
       switch ($linkNum)
       {
          case 0: return array("Google page", "http://www.google.com");
          case 1: return array("Google UK page", "http://www.google.co.uk");
          case 2: return array("Yahoo page", "http://www.yahoo.com");
          case 3: return array("Gmail", "http://www.gmail.com"); 
       }
    }
    
    PHP:
    The function returns an array with the first element a text anchor and the second one the URL.

    You can use this code in any PHP page (if it is included in it of course) e.g.

    $textLink = getTextLink();
    echo "<a href='{$textLink[1]}'>{$textLink[0]}</a>";
    
    PHP:
     
    krzyk, Jan 29, 2008 IP
  3. amelen

    amelen Active Member

    Messages:
    686
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    85
    #3
    Thanks krzyk! I'll try it out.
     
    amelen, Jan 30, 2008 IP
  4. .Darkman

    .Darkman Active Member

    Messages:
    364
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Also, you can use Rotatee (http://rotatee.com) to achieve this. Feel free to sign up using the invite code "gotchance"
     
    .Darkman, Feb 9, 2008 IP