Jump to content


Photo

array_rand help.


  • Please log in to reply
2 replies to this topic

#1 brooksh

brooksh
  • Members
  • PipPipPip
  • Advanced Member
  • 139 posts

Posted 27 October 2006 - 05:24 AM

Here is what I need. I want to pull 10 ids from the database based on their rank. Then pick a random id out of the 10 ids. Right now it pulls 10 ids, but I want it to just pull 1 that is random.  Here is what I have.

$result=mysql_query("SELECT * FROM contacts WHERE callback ='' AND updated ='' AND processing='' AND noanswer='' AND voting='' ORDER BY rank DESC LIMIT 10"); 
if ($myrow = mysql_fetch_array($result)) {
 do
 {
 ?>

<table width=500 align=center><tr><td><? $id = $myrow["id"]; ?>
<body onLoad="document.forms[0].submit()">
  <form method="post" name="radio_form" action="<?php echo $SCRIPT_NAME; ?>?id=<?php echo $id; ?>&processing=1&post=">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<input type="hidden" name="processing" value="1">
</td></tr></table>

 <?php


}
while ($myrow = mysql_fetch_array($result));
}


#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 27 October 2006 - 11:18 AM

<?php

$query = "SELECT id FROM `Table_Name` ORDER BY rank ASC LIMIT 10";
$result = mysql_query($query);
$array=array();

while($row = mysql_fetch_array($result))
  $array[] = $row['id'];

shuffle($array);

echo $array[0];

?>

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 brooksh

brooksh
  • Members
  • PipPipPip
  • Advanced Member
  • 139 posts

Posted 27 October 2006 - 02:27 PM

Thanks, works great.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users