richard_PHP Posted May 30, 2009 Share Posted May 30, 2009 back with, hopefully, the last problem for a while. scenario: im wanting to perform a php rand() function so that random projects are displayed in a page. im wanting to use the id number as the variable but i cant think on how to write it. what's in the database table: $sql = "SELECT 'id', 'img', 'thumb', 'url', 'title', 'description' FROM xxx ORDER BY id"; all im going to display is THUMB and TITLE, but as said i cannot for the life of me think of a way to write it!!! pre-thanks for your help. Link to comment https://forums.phpfreaks.com/topic/160276-solved-rand-help/ Share on other sites More sharing options...
Michdd Posted May 30, 2009 Share Posted May 30, 2009 $sql = "SELECT 'id', 'img', 'thumb', 'url', 'title', 'description' FROM xxx ORDER BY RAND()"; Link to comment https://forums.phpfreaks.com/topic/160276-solved-rand-help/#findComment-845831 Share on other sites More sharing options...
richard_PHP Posted May 30, 2009 Author Share Posted May 30, 2009 no no no! lol.. that bit of code was just to show what the database table contains. im trying to have a crack at it, ill let you know how it goes. Link to comment https://forums.phpfreaks.com/topic/160276-solved-rand-help/#findComment-845833 Share on other sites More sharing options...
Michdd Posted May 30, 2009 Share Posted May 30, 2009 no no no! lol.. that bit of code was just to show what the database table contains. im trying to have a crack at it, ill let you know how it goes. If you're online going to use thumb and title, use this: $sql = "SELECT title, thumb FROM xxx ORDER BY RAND()"; You might also want to set a limit, depending on your situation. Link to comment https://forums.phpfreaks.com/topic/160276-solved-rand-help/#findComment-845834 Share on other sites More sharing options...
richard_PHP Posted May 30, 2009 Author Share Posted May 30, 2009 got it working! that was easier than i first thought it was. looking back, i could have been a bit clearer with what i wanted. but here's the final code: <?php $random = rand(1,5); $conn = mysql_connect("xxxx", "xxx", "xxx"); mysql_select_db("xxx", $conn); $sql = "SELECT * FROM xxx WHERE id = '$random'"; $result = mysql_query($sql, $conn); $array = mysql_fetch_array($result); echo "<p><img src=".$array[thumb]." /><br />"; echo "<a href='info.php?id=$random'>".$array[title]."</a></p>"; ?> thanks for the help though! =D Link to comment https://forums.phpfreaks.com/topic/160276-solved-rand-help/#findComment-845837 Share on other sites More sharing options...
Michdd Posted May 30, 2009 Share Posted May 30, 2009 got it working! that was easier than i first thought it was. looking back, i could have been a bit clearer with what i wanted. but here's the final code: <?php $random = rand(1,5); $conn = mysql_connect("xxxx", "xxx", "xxx"); mysql_select_db("xxx", $conn); $sql = "SELECT * FROM xxx WHERE id = '$random'"; $result = mysql_query($sql, $conn); $array = mysql_fetch_array($result); echo "<p><img src=".$array[thumb]." /><br />"; echo "<a href='info.php?id=$random'>".$array[title]."</a></p>"; ?> thanks for the help though! =D That's extremely unnecessary, and will only get a real random value if it's between the value you hard coded.. I told you use the RAND() mysql function to grab a random one. If you only want to choose one do this: $sql = "SELECT title, thumb FROM xxx ORDER BY RAND() LIMIT 1"; It's faster, and more 'correct' Link to comment https://forums.phpfreaks.com/topic/160276-solved-rand-help/#findComment-845839 Share on other sites More sharing options...
richard_PHP Posted May 30, 2009 Author Share Posted May 30, 2009 thanks. implemented it and it seems simpler than what i did. many thanks again. =D Link to comment https://forums.phpfreaks.com/topic/160276-solved-rand-help/#findComment-845842 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.