Help me pick up random results as required

Discussion in 'PHP' started by ketting00, Aug 27, 2011.

  1. #1
    Hi,
    I found a php snippet bellow through google and it works fine in picking up results at random. However, some time the results is smaller than I wanted it be, and some times as bad as 1 result.

    
    $count = mysql_query("SELECT count(*) FROM featured");
    $sql = mysql_fetch_row($count);
    $random = mt_rand(0,$sql[0] - 1);
    
    $result = "mysql_query("SELECT id, name FROM featured LIMIT $random, 12")
    
    Code (markup):
    How could I always get 12 results out of the random?

    Thanks in advance
     
    ketting00, Aug 27, 2011 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    Would need to see more of the search code, for example are you searching for exact "word" or have you tried using words like "%word%" witch is a wild card or words within words, you can mix that as-well like "word%" so it grabs the beginning word plus anything else ending like "wording"

    Its just another way to get more results and your code above will always limit to 12 regardless


    word = word

    %word% = any wording

    word% = wording

    %word = any word
     
    Last edited: Aug 27, 2011
    MyVodaFone, Aug 27, 2011 IP
  3. insert

    insert Peon

    Messages:
    148
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    you switch the arguments of the limit clause, it should be $result = "mysql_query("SELECT id, name FROM featured LIMIT 12, $random")
     
    insert, Aug 28, 2011 IP
  4. insert

    insert Peon

    Messages:
    148
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    you should also check if the $random + 12 isn't bigger then $sql[0]...
     
    insert, Aug 28, 2011 IP
  5. ketting00

    ketting00 Well-Known Member

    Messages:
    782
    Likes Received:
    28
    Best Answers:
    3
    Trophy Points:
    128
    #5
    No, this ain't good. The result sometime come out more than 12. My web page design fits for 12 items or static result, other wise it would look ugly.
     
    ketting00, Aug 28, 2011 IP
  6. Technoslab

    Technoslab Peon

    Messages:
    46
    Likes Received:
    3
    Best Answers:
    3
    Trophy Points:
    0
    #6
    
    $result = "mysql_query("SELECT id, name FROM featured order by RAND() LIMIT 12");
    
    Code (markup):
     
    Technoslab, Aug 28, 2011 IP