james19 Posted August 19, 2007 Share Posted August 19, 2007 Hi, I am trying to create a webpage that generates 5 random numbers from a list that is produced by a database query. I know that the randomisation works as i have tested it. Listing the numbers from the database also works as it displays them. The random code is as follows: <?php $numbers=array(1,2,3,4,5,6,7,8,9,10); //Get 5 unique random keys from $numbers array. $rand_keys = array_rand($numbers, 5); //if you want to sort the random keys sort($rand_keys); //print out the random numbers using the //random keys. foreach ($rand_keys as $k=>$v) { echo $numbers[$v].", "; } ?> The mysql query is as follows: <?php require_once ('connect.php'); // Connect to the database. // Generate a list of all of the sponsorhip_ids $query_ids = mysql_query("SELECT id FROM images WHERE type_id='5' AND active='Y' ORDER BY sponsorship_id"); while ($all_ids = mysql_fetch_array ($query_ids, MYSQL_NUM)) { $all_ids[0]; $content.=$all_ids[0].','; } echo 'Listed ids from table: '.$content.'<br>'; // print the results to check results (4 testing) $desc_len = strlen($content); $content[$desc_len-1] = ''; //remove the remaining comma $array_content = strip_tags($content); echo $array_content.'<br><br>'; // print the results to check results after the final commer has been removed (4 testing) ?> Ok; now for the combined code, and where i am having the problems <?php require_once ('connect.php'); // Connect to the database. // Generate a list of all of the sponsorhip_ids $query_ids = mysql_query("SELECT id FROM images WHERE type_id='5' AND active='Y' ORDER BY sponsorship_id"); while ($all_ids = mysql_fetch_array ($query_ids, MYSQL_NUM)) { $all_ids[0]; $content.=$all_ids[0].','; } echo 'Listed ids from table: '.$content.'<br>'; // print the results to check results (4 testing) $desc_len = strlen($content); $content[$desc_len-1] = ''; //remove the remaining comma $array_content = strip_tags($content); echo $array_content.'<br><br>'; // print the results to check results after the final commer has been removed (4 testing) $numbers = array($array_content); //Get 5 unique random keys from $numbers array. $rand_keys = array_rand($content, 5); //if you want to sort the random keys sort($rand_keys); //print out the random numbers using the //random keys. foreach ($rand_keys as $k=>$v) { echo $numbers[$v].", "; } ?> The $array_content has a list of 10 numbers as follows: 2,5,8,9,10,15,22,23,24,26 When the script is fuly run it comes up with the errors: Warning: array_rand(): First argument has to be an array in /home/www/kd-uk.freehostia.com/test-delete/select_rand2.php on line 70 Warning: sort() expects parameter 1 to be array, null given in /home/www/kd-uk.freehostia.com/test-delete/select_rand2.php on line 73 Warning: Invalid argument supplied for foreach() in /home/www/kd-uk.freehostia.com/test-delete/select_rand2.php on line 77 I presume these errors occur because the $array_content is not printed ie the coding does not look like this: $numbers=array(2,5,8,9,10,15,22,23,24,26) Is there a way of getting it so it does? Please help... Many Thanks James Link to comment https://forums.phpfreaks.com/topic/65688-generate-random-numbers-from-a-mysql-result/ Share on other sites More sharing options...
trq Posted August 19, 2007 Share Posted August 19, 2007 Replace... $array_content = strip_tags($content); with $array_content = explode(',',strip_tags($content)); Link to comment https://forums.phpfreaks.com/topic/65688-generate-random-numbers-from-a-mysql-result/#findComment-328067 Share on other sites More sharing options...
sasa Posted August 19, 2007 Share Posted August 19, 2007 try <?php require_once ('connect.php'); // Connect to the database. // Generate a list of all of the sponsorhip_ids $query_ids = mysql_query("SELECT id FROM images WHERE type_id='5' AND active='Y' ORDER BY sponsorship_id"); while ($all_ids = mysql_fetch_array ($query_ids, MYSQL_NUM)) { //$all_ids[0]; $content[] = $all_ids[0]; //$content.=$all_ids[0].','; } //echo 'Listed ids from table: '.$content.'<br>'; // print the results to check results (4 testing) //$desc_len = strlen($content); //$content[$desc_len-1] = ''; //remove the remaining comma //$array_content = strip_tags($content); print_r($content); //echo $array_content.'<br><br>'; // print the results to check results after the final commer has been removed (4 testing) //$numbers = array($array_content); $numbers = $array_content; //Get 5 unique random keys from $numbers array. $rand_keys = array_rand($content, 5); //if you want to sort the random keys sort($rand_keys); //print out the random numbers using the //random keys. foreach ($rand_keys as $k=>$v) { echo $numbers[$v].", "; } ?> Link to comment https://forums.phpfreaks.com/topic/65688-generate-random-numbers-from-a-mysql-result/#findComment-328069 Share on other sites More sharing options...
MadTechie Posted August 19, 2007 Share Posted August 19, 2007 just wondering why not do something simple like SELECT * FROM `images ` WHERE active='Y' ORDER BY RAND() ASC LIMIT 0,4 Link to comment https://forums.phpfreaks.com/topic/65688-generate-random-numbers-from-a-mysql-result/#findComment-328072 Share on other sites More sharing options...
james19 Posted August 19, 2007 Author Share Posted August 19, 2007 Thank you for all your help.... However, I stuggled to get any of the suggestions to work (they still came up with the same errors... just wondering why not do something simple like SELECT * FROM `images ` WHERE active='Y' ORDER BY RAND() ASC LIMIT 0,4 - because i want to generate 5 unique results, so it does not deliver 2 of the same results Link to comment https://forums.phpfreaks.com/topic/65688-generate-random-numbers-from-a-mysql-result/#findComment-328089 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.