conker87 Posted June 23, 2010 Share Posted June 23, 2010 This is a script that I've edited a little bit to suit my needs, however, it's not working at all. It's not echoing the correct value (it's just echoing "1"). There's obviously something wrong, I just can't put my finger on it. All the print_r()'s are there for debugging, trying to find out what is wrong! require("_mysql.php"); $query = "SELECT COUNT(*) FROM `adverts` WHERE `align` = 'hor'"; $result = mysql_query($query); $banner_no = mysql_fetch_array($result); $query = "SELECT * FROM `adverts` WHERE `align` = 'hor'"; $result = mysql_query($query); $row = mysql_fetch_array($result);; $no = rand(0, $banner_no[0]); $hor_ads = $row[$no]; echo $hor_ads['code']; Quote Link to comment https://forums.phpfreaks.com/topic/205648-simple-banner-roation-script/ Share on other sites More sharing options...
premiso Posted June 23, 2010 Share Posted June 23, 2010 $query = "SELECT * FROM adverts WHERE align = 'hor' order by rand() LIMIT 1"; $result = mysql_query($query) or trigger_error("Adverts query failed Error: " mysql_error()); $banner = mysql_fetch_assoc($result) or trigger_error("Unable to fetch Adverts Result"); echo $banner['code']; Should work for random images using MySQL rand() function. Quote Link to comment https://forums.phpfreaks.com/topic/205648-simple-banner-roation-script/#findComment-1076171 Share on other sites More sharing options...
conker87 Posted June 23, 2010 Author Share Posted June 23, 2010 Fantastic, thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/205648-simple-banner-roation-script/#findComment-1076175 Share on other sites More sharing options...
Cagecrawler Posted June 23, 2010 Share Posted June 23, 2010 order by rand() can get very expensive in terms of performance if you have a lot of rows. I prefer to use: SELECT * FROM table WHERE num_value >= RAND() * (SELECT MAX(num_value) FROM table) LIMIT 1; Of course, if you're dealing with tens of rows rather than thousands, it isn't going to make much difference. Quote Link to comment https://forums.phpfreaks.com/topic/205648-simple-banner-roation-script/#findComment-1076179 Share on other sites More sharing options...
conker87 Posted June 23, 2010 Author Share Posted June 23, 2010 At the minute, there are only 2 rows and I don't expect them to get higher than 20 to be honest. Quote Link to comment https://forums.phpfreaks.com/topic/205648-simple-banner-roation-script/#findComment-1076181 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.