guyfromfl Posted January 11, 2008 Share Posted January 11, 2008 I don't understand why this doesn't work for background...I want to make a script to randomly choose a flyer picture for upcomming shows for a band. php knows theres something comming back from mysql because $display always has a value, just no picture.. $sql = "SELECT loc FROM flyers WHERE showDate >= curdate()"; $result = mysql_query($sql); $flyer = array(); $ct = 1; while ($row = mysql_fetch_array($result)) { $flyer[$ct] = $row[1]; $ct++; } $display = rand(1,(sizeof($flyer))); print "<img src=$flyer[$display]>" . $display . $flyer[$display]; you can see the output of that here http://alchemy.servebeer.com/test/ad.php Link to comment https://forums.phpfreaks.com/topic/85568-solved-pop-array-from-mysql/ Share on other sites More sharing options...
Psycho Posted January 11, 2008 Share Posted January 11, 2008 You can use MySQL to select a random record, which makes this much easier: $sql = "SELECT loc FROM flyers WHERE showDate >= curdate() ORDER BY rand() LIMIT 1"; $result = mysql_query($sql); $randomRecord = mysql_fetch_array($result)); print "<img src=$randomRecord[0]>" . $randomRecord[0]; EDIT: Your problem is that you are only selecting 1 item from MySQL and you are creating your array based on "$row[1];". There is no value for that, the first value would be at index 0. Link to comment https://forums.phpfreaks.com/topic/85568-solved-pop-array-from-mysql/#findComment-436659 Share on other sites More sharing options...
wildteen88 Posted January 11, 2008 Share Posted January 11, 2008 What is outputted when you add: echo '<pre>' . print_r($flyer, true) . '</pre>'; After your while loop Link to comment https://forums.phpfreaks.com/topic/85568-solved-pop-array-from-mysql/#findComment-436661 Share on other sites More sharing options...
kopytko Posted January 11, 2008 Share Posted January 11, 2008 <?php $sql = "SELECT loc FROM flyers WHERE showDate >= curdate()"; // ............................. $flyer[$ct] = $row[0]; ?> The first value in $row is indexed from 0, not from 1. Link to comment https://forums.phpfreaks.com/topic/85568-solved-pop-array-from-mysql/#findComment-436662 Share on other sites More sharing options...
guyfromfl Posted January 11, 2008 Author Share Posted January 11, 2008 thanks guys...it was the array start thing. I tried the MySQL rand() function but didnt get it to work, ill look more into it tho! Link to comment https://forums.phpfreaks.com/topic/85568-solved-pop-array-from-mysql/#findComment-436664 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.