SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Random link

  1. #1
    ********* Poet X Q mano's Avatar
    Join Date
    Oct 2002
    Location
    Still Thulcandra
    Posts
    997
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Random link

    Hi,

    It's been a while since I've been here, but I need an advice for a pretty basic thing.

    I have a db of a few links that I want to display a random one at one page, and several random on another page, and none of them should be the same. (On the page that I show more at a time)

    The db is extremely basic, with linkid, linkname and linktext...

    I've done a search here, but can't find it btw...

  2. #2
    American't awestmoreland's Avatar
    Join Date
    Sep 2002
    Location
    Grand Rapids, MI
    Posts
    1,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Seemed like an interesting challenge so here's my attempt at it:
    PHP Code:
    <?php
    # Server Settings
    $mssql_server     'localhost';
    $mysql_server_username '';
    $mysql_server_password '';
     
    # Database Settings
    $mysql_db        'randomlinks';
    $mysql_table     'linktable'
     
    # Routine Configuration
    $maxUnique     3# Maximum number of unique links to display
    $foundUnique     "0"# Number of unique numbers found
    $usedNums        ""# Unique numbers used
     
    # Connect to Server
    mysql_connect($mssql_server,$mssql_server_username,$mssql_server_password) or die ("<div class='error'>ERROR: Unable to connect to server</div>");
     
    # Select DB 
    mysql_select_db($mysql_db) or die ("<div class='error'>ERROR: Unable to open database<br />MySQL Says: " mysql_error());
     
    # Count records in table ($linkCount)
    $result mysql_query("SELECT count(*) AS linkCount from linktable");
    $linkCount mysql_result($result,0,"linkCount");
     
    while(
    $foundUnique $maxUnique){
      
    $linkNum rand(0,$linkCount-1);
      if(
    strpos($usedNums"(".$linkNum.")")===false){
        
    $usedNums .= "(".$linkNum.")";
        
    $foundUnique++;
        
    $linkRecordArray=mysql_query("SELECT * FROM linktable LIMIT ".$linkNum.",1;");
        while(
    $row mysql_fetch_array($linkRecordArray)){
          echo(
    '<a href="'.$row[2].'">'.$row[1].'</a><br>');
        } 
      }
    }
     
    # Explanation of above code: 
    # Do while $foundUnique < $maxUnique
    #   Generate random number between 0 and number of records in table ($linkNum)
    #   Check "(".$linkNum.")" doesn't exist in string $usedNums
    #   If yes
    #     do nothing
    #   End If
    #   If no
    #     Add "(".$linkNum.")" to string $usedNums
    #     Increment $foundUnique
    #     Get record number $linkNum from DB and output as link
    #   End If
    # End While
    ?>
    I've left the server/DB settings in for you to edit for ease of testing but it works for me on the following data:
    PHP Code:
    # Table Settings
    $add_table "CREATE TABLE IF NOT EXISTS ".$mysql_table." (
        linkid         SMALLINT NOT NULL AUTO_INCREMENT,
        linkname     CHAR(20) NOT NULL,
        linkref        CHAR(100) NOT NULL,
        PRIMARY KEY    (linkid))"
    ;
     
    # Table Data
    $insert_data "INSERT INTO ".$mysql_table." (linkname,linkref) VALUES
        ('The Company','http://www.the-company.com'),
        ('B3ta','http://www.b3ta.com'),
        ('Badgers etc.','http://www.jolt.co.uk'),
        ('SitePoint Forums','http://www.sitepoint.com/forums'),
        ('BBC News','http://news.bbc.com'),
        ('Project VoteSmart','http://www.vote-smart.org/'),
        ('Dictionary','http://dictionary.reference.com');"

    I'm sure that there must be a better way of ensuring that each random number selected is unique, but as I say - this works.


    Andy
    Last edited by awestmoreland; Feb 20, 2004 at 08:13.
    From the English nation to a US location.

  3. #3
    SitePoint Wizard Lats's Avatar
    Join Date
    Jun 2003
    Location
    Melbourne, AU
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by awestmoreland
    I'm sure that there must be a better way of ensuring that each random number selected is unique, but as I say - this works.
    That's a great answer, phew

    I show 4 random links by using...
    PHP Code:
        $sql "
            SELECT
                *
            FROM
                links
            ORDER BY
                RAND()
            LIMIT 4"

    Works for me
    Lats...

  4. #4
    American't awestmoreland's Avatar
    Join Date
    Sep 2002
    Location
    Grand Rapids, MI
    Posts
    1,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    D'oh! <slaps head/>
    I hadn't considered sorting the table contents during the select.

    Nobody likes a smartass you know

    Seriously though, I bow to your conciseness <polite applause/>


    Andy
    From the English nation to a US location.

  5. #5
    ********* Poet X Q mano's Avatar
    Join Date
    Oct 2002
    Location
    Still Thulcandra
    Posts
    997
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot guys!

  6. #6
    masquerading Nick's Avatar
    Join Date
    Jun 2003
    Location
    East Coast
    Posts
    2,215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    /me looks at awestmoreland's answer, than at lats.

    /me giggles

    Erm, uhh, laughs...
    Nick . all that we see or seem, is but a dream within a dream
    Show someone you care, send them a virtual flower.
    Good deals on men's watches


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •