MySQL: How to Order by Time then Choose a Random

Discussion in 'Programming' started by unl0ad, Mar 31, 2007.

  1. #1
    Ok i got a simple problem... What im trying to do is grab the newest 15 links which I have a TIME table for, then out of those new 15 i want to choose 1 random link. The code below works, but it grabs 15 randoms out of the entire 200 link list then chooses one.


    $result=mysql_query("select * from babeloggerv3 where approved = 'yes'  AND '".time()."'>=time ORDER BY RAND() LIMIT 1, 15"); 
    while($row=mysql_fetch_array($result)){header("Location: http://www.needbang.com/plugs/out.php?id=$row[id]");}
    
    Code (markup):


    -thanks in advance

    -tommy
     
    unl0ad, Mar 31, 2007 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    I would grab the newest 15 links from the database without using rand, then use php functions or your own function to choose a random id from that array, or maybe theres another way, I'm v drunk, I'd write some code but it would prolly turn out japanese we'll see how you're doing in the morning .....
     
    krakjoe, Mar 31, 2007 IP
  3. unl0ad

    unl0ad Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    umm if only i knew how to do that ;) perhaps you can guide me in the right direction...
     
    unl0ad, Mar 31, 2007 IP
  4. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #4
    nico is here, just wait a couple of minutes .....
     
    krakjoe, Mar 31, 2007 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    EDIT: ^^ Hahah. :D

    Try
    
    $result=mysql_query("select id from babeloggerv3 where approved = 'yes'  AND '".time()."'>=time LIMIT 1, 15");
    
    header("Location: http://www.needbang.com/plugs/out.php?id=" . mysql_result($result, rand(0, 14)));
    
    PHP:
     
    nico_swd, Mar 31, 2007 IP
  6. unl0ad

    unl0ad Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    ;P pure genius, worked like a charm
     
    unl0ad, Mar 31, 2007 IP