February 26, 201016 yr Hello friends, pls i need to display different images at each time a visitor visit my webpage. i have my images in the database but i dont know how to each/display them at random? pls help. regards
February 26, 201016 yr How is your database set up? What are the fields? If you have a database which is JUST for images and it has a unique integer ID then you could set a variable: $my_image = rand(1,100); and then select via SQL using something like: SELECT image WHERE img_id = $my_image I am no expert - but if only wanted to display a random image then do you need them in a DB? Couldn't javascript do the same job?
March 1, 201016 yr Terydhino has the right answer however there is a simpler way of doing it: mysql_query("SELECT image FROM table ORDER BY RAND() LIMIT 1"); That will randomly select one `image` from the table `table`.
March 1, 201016 yr MySQL has some weird and wonderful functions. If something looks over complicated, it probably is. Thanks for the +1
March 3, 201016 yr You will find this method below to be much much much much much much quicker than using mysql to generate the random number. You wont notice it if you only have a few hundred records but if you have thousands you will definitely want to use the method stated by terydinho below rather then the MYSQL method. Iv tried both on record sets of 10,000 and it is noticably faster to use php to generate a random number first then use that. How is your database set up? What are the fields? If you have a database which is JUST for images and it has a unique integer ID then you could set a variable: $my_image = rand(1,100); and then select via SQL using something like: SELECT image WHERE img_id = $my_image I am no expert - but if only wanted to display a random image then do you need them in a DB? Couldn't javascript do the same job?
Create an account or sign in to comment