PHP script help

Discussion in 'PHP' started by brakza, Jan 31, 2011.

  1. #1
    Hi i am making a new site, and i need someone helping me build a script that, could randomly appear images, and so you can vote on the hottest picture, quite simmalare to Hotornor.com!

    Could rly need some help :)
     
    brakza, Jan 31, 2011 IP
  2. militarysmurf

    militarysmurf Member

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    40
    #2
    select * from images order by rand();
     
    militarysmurf, Jan 31, 2011 IP
  3. prefiber

    prefiber Peon

    Messages:
    26
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    or you select all images from mysql, place them in a array and use then array_rand()
     
    prefiber, Jan 31, 2011 IP
  4. salmanshafiq

    salmanshafiq Well-Known Member

    Messages:
    260
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    128
    #4
    You can put all images names in an array and show using $arr[rand(0,sizeof($arr))].
     
    salmanshafiq, Jan 31, 2011 IP
  5. srisen2

    srisen2 Peon

    Messages:
    359
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You could always just go to the next image in the line as to not repeat images with the mysql approach. Simply use RND to start the sequence at a random spot in the archive but rotate the images in the databases set order.
     
    srisen2, Jan 31, 2011 IP
  6. wakkawakka

    wakkawakka Member

    Messages:
    58
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #6
    i also think this is best
     
    wakkawakka, Feb 1, 2011 IP
  7. W3Theory

    W3Theory Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You shouldn't use ORDER BY RAND().
    It's very inefficient, it re-queries the data and assigns an ID and then displays the data.
     
    W3Theory, Feb 1, 2011 IP
  8. brakza

    brakza Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Yeah and i need a voting system! so if its random the votes would not get saved. So i need another one.
     
    brakza, Feb 2, 2011 IP
  9. W3Theory

    W3Theory Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    $Data = mysql_query("SELECT `ID`, `Image` FROM Table", $ConnectionID) or die(mysql_error());
    $Count = mysql_num_rows($Data);
    if($Count > 0) {
    $ID = array();
    while($Fetch = mysql_fetch_assoc($Data)) {
    $ID[] = $Fetch['ID']
    }
    $Select = mysql_fetch_assoc(mysql_query("SELECT `ID`, `Image` FROM Table WHERE `ID` = ".$ID[rand(0, (count($ID)-1))]."", $ConnectionID));
    $Image = $Select['Image'];
    }

    There, random image....
     
    W3Theory, Feb 2, 2011 IP
  10. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #10
    @W3Theory, even your method will get slow if the table contains thousands of image. Looping through each image for creating an array will take time, more over, it may as well overload server in terms of memory and processor utilization.

    Ideal way would be:
    1. Make a query to select minimum and maximum id from table.
    SELECT MIN(id), MAX(id) FROM table_name
    2. Using PHP generate a random number between min and max id.
    3. Make a query to select one row present at that id or greater.
    SELECT COLUMN_NAMES FROM table_name WHERE id >= random_id

    This would make it very fast.
    Note: Above logic is based on assumption that table contains a unique column id or similar.
     
    mastermunj, Feb 2, 2011 IP