Jump to content

generate random numbers from a mysql result


james19

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.