Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Order Select

Status
Not open for further replies.

MrPantsTm

Programmer
Jan 22, 2004
71
US
I have a user database which I use for meetings. What it does is it creates notes for everyone thats going to be in the meeting. Right now, the order people give notes is dependant on the order the select statement returns (right now I have it as desc using the ID#). My boss would like it if it could "randomly" arrange the users. i.e

Day 1:
1 - Bob
3 - John
2 - Beth
5 - Susie
....

Day 2:
5 - Susie
2 - Beth
1 - Bob
3 - John
....

etc.. So no one feels like they are being picked on or it doesn't get stagnant.

What would be nice would be "Select blah blah Order by Random" but I don't think thats an option. Is there anything in mysql to do this or is this something I'm going to have to manipulate after I have the data out of the database? TIA!
 
would mysql understand that? I didn't think random was a valid mysql variable?
 
actually, upon reexamining the code, I don't use the select. I do however, have the information in an array. Is it possible to do something like the above with an array?
 
I'm sorry. This is in the MySQL forum but is a PHP question. I wanted to use the same thread for history.

Using PHP, I have an array of names which need to be pumped out in random order with no duplicates. I'm thinking of having a second array which will hold a list of which ones have been used but this doesn't seem elegant to me so I'm hoping someone can point me to a better solution.
 
I generally pick an element at random, output it, then remove it from the array. Then keep doing this until there are no more elements in the array.

Code:
<?php
$names = array ('Alice', 'Bob', 'Charline', 'Doug', 'Ella', 'Fred', 'Georgia', 'Henry');

while (count($names) > 0)
{
	$index = rand(0, count($names) - 1);

	reset ($names);
	$count = 0;
	while ($count < $index)
	{
		next($names);
		$count++;
	}
	print current($names) . '<br>';
	unset($names[key($names)]);
}
?>

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top