steviez Posted March 5, 2007 Share Posted March 5, 2007 Hi, I am wanting to display 3 random music tracks stored in my databe on my front page, i am using the following code: <?php include("connect.php"); include("maintenance_switch.php"); if($DOWN == 'TRUE'){ header("Location:maintenance.php"); } $query_featured1 = "select * from audio where featured = 'YES' order by RAND() LIMIT 1,1"; $result_featured1 = mysql_query($query_featured1); $row_featured1 = mysql_fetch_array($result_featured1); $query_featured2 = "select * from audio where featured = 'YES' order by RAND() LIMIT 1,2"; $result_featured2 = mysql_query($query_featured2); $row_featured2 = mysql_fetch_array($result_featured2); $query_featured3 = "select * from audio where featured = 'YES' order by RAND() LIMIT 1,3"; $result_featured3 = mysql_query($query_featured3); $row_featured3 = mysql_fetch_array($result_featured3); if($row_featured1['audio_image'] == '') { $display_audio_image = $DEFAULT_AUDIO_IMAGE; } else { $display_audio_image = $row_featured1['audio_image']; } if($row_featured2['audio_image'] == '') { $display_audio_image = $DEFAULT_AUDIO_IMAGE; } else { $display_audio_image = $row_featured2['audio_image']; } if($row_featured3['audio_image'] == '') { $display_audio_image = $DEFAULT_AUDIO_IMAGE; } else { $display_audio_image = $row_featured3['audio_image']; } ?> The only thing is.. the images are all the same most of the time and i need it so that there is never two of the same tracks shown at the same time. Any ideas? Thanks Link to comment https://forums.phpfreaks.com/topic/41304-dont-have-a-clue-what-to-do/ Share on other sites More sharing options...
mrheff Posted March 5, 2007 Share Posted March 5, 2007 maybe you could make a list that appears random, that is very long out this in your database, and when it reads from the list you get them without repitition but it appears random to the user, i dont know how to implement this but it might be a method if all else fails heff. Link to comment https://forums.phpfreaks.com/topic/41304-dont-have-a-clue-what-to-do/#findComment-200129 Share on other sites More sharing options...
steviez Posted March 5, 2007 Author Share Posted March 5, 2007 Thanks for that, the only thing is that im pretty new to PHP and its all a bit daunting. Link to comment https://forums.phpfreaks.com/topic/41304-dont-have-a-clue-what-to-do/#findComment-200131 Share on other sites More sharing options...
chronister Posted March 5, 2007 Share Posted March 5, 2007 You might try combining all 3 queries into 1 query changing the limit to 1,3. I am not extremely familiar with the rand() function, but I would imagine that grabbing 3 items in 1 query rather than 1 item in 3 queries would produce less of a chance of duplication. <?php $query_featured = "select * from audio where featured = 'YES' order by RAND() LIMIT 1,3"; $result_featured = mysql_query($query_featured); while($row_featured = mysql_fetch_array($result_featured)){ if($row_featured['audio_image'] == '') { $display_audio_image = $DEFAULT_AUDIO_IMAGE; } else { $display_audio_image = $row_featured['audio_image']; } } ?> Try that, hope it helps. Link to comment https://forums.phpfreaks.com/topic/41304-dont-have-a-clue-what-to-do/#findComment-200205 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.