Random Image

Discussion in 'PHP' started by MajHate, Aug 17, 2009.

  1. #1
    Hello,

    I am working on a script where random images show up and people rate them. I am having one problem though. That is that the same images keep showing up! Does anyone know how to go about this? I was thinking about using cookies but I am totally lost! Any ideas?

    Thank You!
     
    MajHate, Aug 17, 2009 IP
  2. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #2
    Where are you getting the images from? Could you provide the code you are currently using?

    Would be helpful.
     
    Sky AK47, Aug 17, 2009 IP
  3. MajHate

    MajHate Member

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    the images are just in a folder called images. The only code i have is to select a random picture from my database, thats all I know how to do so far.

    
    $query = "SELECT * FROM pictures";
    $result = MYSQL_QUERY($query);
    $randval = rand(0, mysql_num_rows($result) - 1);
    $Rand_PID = mysql_result($result, $randval, 0); // Gets the Picture ID number
    
    PHP:
     
    MajHate, Aug 17, 2009 IP
  4. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #4
    Ah, ok.
    Loop through the dir with opendir and readdir and add the files to a array.
    When you did that, use rand.
     
    Sky AK47, Aug 17, 2009 IP
  5. MajHate

    MajHate Member

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    I don't quite understand. I am new to php, could you explain a bit more please?

    thank you!
     
    MajHate, Aug 17, 2009 IP
  6. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #6
    Try something like this:
    <?php
    $directory = '/images/';
    if ($handle = opendir($directory)){
        $files = $array();
        while (false !== ($file = readdir($handle))){
            $files[] = $file;
        }
    }
    $random_image = rand(0, (count($files)-1));
    ?>
    PHP:
     
    Sky AK47, Aug 17, 2009 IP
  7. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #7
    If you really need to fetch image as an SQL resut you could try something like this:
    
    // Count total number of rows and place it in $records
    $query = "SELECT COUNT(*) as count FROM pictures";
    list ($records) = mysql_fetch_assoc(mysql_query($query));
    
    // Generate random offset (0<$offset<$records) for query
    $offset = rand(0, $records - 1);
    
    // Finally, fetch random row result
    $query = "SELECT * FROM pictures LIMIT {$offset},1";
    $Rand_PID = mysql_fetch_assoc(mysql_query($query));
    
    PHP:
     
    Gray Fox, Aug 17, 2009 IP
  8. dannywwww

    dannywwww Well-Known Member

    Messages:
    804
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    110
    #8
    What about having a query like this, and use the RAND() function in MySQL.

    
    $query = mysql_query("SELECT * FROM `pictures` ORDER BY RAND() LIMIT 1;") or die(mysql_error());
    
    PHP:
    then basically, fetch the information of that image.
     
    dannywwww, Aug 17, 2009 IP
  9. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #9
    That solution seems definitely better than mine, I actually didn't know that the RAND() function can be used in that way.
     
    Gray Fox, Aug 17, 2009 IP
  10. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #10
    The more rows there are in your database table, the worse ORDER BY RAND will perform.

    Selecting a row based on the count of rows in a table times a random float works great. this seems to work a little faster though because it's using one query instead of two and the second query takes the same amount of time as a normal WHERE id=N query. :)
     
    joebert, Aug 17, 2009 IP
  11. silotka

    silotka Peon

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    I will say this will be better:
    1) get picture count
    2) do random from 1 to picture count
    3) get picture by random selected id

    maybe it will lower server load!
     
    silotka, Aug 18, 2009 IP
  12. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Until you start noticing that your site is slow, it's best to just use the standard "ORDER BY RAND()" solution.

    Many people claim this is slow, and it was, but I don't know if it still is. Has anyone actually bothered to test this again in newer versions of mysql? It would seem to me that this would be something the mysql guys would bother optimizing (if it is at all possible)
     
    premiumscripts, Aug 18, 2009 IP
  13. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #13
    People, I provided the solution. Stop spamming the same answer...
     
    Sky AK47, Aug 18, 2009 IP
  14. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #14
    It's not the same answer, it's how to do it if you have to use MySQL resource. This is not a competition, Sky AK47, everyone is trying their best.
     
    Gray Fox, Aug 19, 2009 IP
  15. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #15
    But he doesn't use MySQL if you can read :)
     
    Sky AK47, Aug 19, 2009 IP
  16. dannywwww

    dannywwww Well-Known Member

    Messages:
    804
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    110
    #16
    And if you could read, you may be able to read the code he posted here. He does use MySQL from the snippet of code he posted.
     
    dannywwww, Aug 19, 2009 IP
  17. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Doh, I think most people misread because he posted a mysql code sample :)

     
    premiumscripts, Aug 19, 2009 IP
  18. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #18
    But he didn't write the preferred solution, MySQL or directory reading, in which case the globbing could also do the trick
    
    $rndImg = $rndImg[array_rand($rndImg = glob("images/*"))];
    
    PHP:
     
    Gray Fox, Aug 19, 2009 IP
  19. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #19
    I can read, too bad you can't :(
    Take a look at what premiumscripts posted.

    @Gray Fox: there are various way to do this, but at least your back on topic!
     
    Sky AK47, Aug 19, 2009 IP