Jump to content


Photo

array randomizer


  • Please log in to reply
3 replies to this topic

#1 willwill100

willwill100
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 30 March 2006 - 09:14 PM

if ($cycling=="RANDOM TEAM!"){
srand((float) microtime() * 10000000);

$putin = "SELECT * FROM teams";
$mallow = mysql_query($putin) or die(mysql_error());
$cap = mysql_fetch_array($mallow) or die (mysql_error());

print_r($cap);

$rand_keys = array_rand($cap, 1);

echo $cap[$rand_keys[0]] . "\n";
}

i have a table `teams` with (ID, team, rating) columns. i just want a random team to be outputted but not a lot is happening, wats wrong?

#2 realjumper

realjumper
  • Members
  • PipPipPip
  • Advanced Member
  • 399 posts

Posted 30 March 2006 - 09:40 PM

What about doing it like this?

// Retrieve all the data from the table
$result = mysql_query("SELECT * FROM teams ORDER BY RAND()")
or die(mysql_error());

// store the record of the table into $row
$row = mysql_fetch_array( $result );

echo "$row[team]";


#3 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 30 March 2006 - 10:01 PM

Use LIMIT to send back a single row. Otherwise you waste a lot of time.

// Retrieve all the data from the table
$result = mysql_query("SELECT * FROM teams ORDER BY RAND() LIMIT 1")
or die(mysql_error());

// store the record of the table into $row
$row = mysql_fetch_array( $result );

echo "$row[team]";


#4 realjumper

realjumper
  • Members
  • PipPipPip
  • Advanced Member
  • 399 posts

Posted 30 March 2006 - 10:05 PM

Quite true....thanks....I forgot that important bit




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users