PDA

View Full Version : random sql



jr_yeo
07-25-2006, 04:27 AM
how to randomize unique id from table? :p

i have id, path, file as field names :D

i want something like

<option value="path/file">random id</option>

Twey
07-25-2006, 12:29 PM
I don't quite understand. Do you want to select a random existing ID from the table, or create a new one?

jr_yeo
07-25-2006, 12:41 PM
Do you want to select a random existing ID from the table

sorry :D existing random id from table :p

Twey
07-25-2006, 02:38 PM
$rs = mysql_query('select id from tablename;');
$randomId = array();
while($row = mysql_fetch_array($rs))
array_push($randomId, $row['id']);
$randomId = $randomId[rand(0, count($randomId))];

jr_yeo
07-25-2006, 03:13 PM
sorry :( but where will i put it? :p


<?php
$result = mysql_query($qStr);
$NumRows = mysql_num_rows($result);
?><body bgcolor="black" text="white" alink="green" vlink="red" link="blue">
<form name="dynamicselector">
<table border="0" width="100%" cellspacing="0" cellpadding="0" height="178">
<tr>
<td width="35%" valign="top" align="left">
<select name="dynamicselector2" size="<?= strstr($_SERVER["HTTP_USER_AGENT"], "MSIE") != false ? "1" : "2" ?>" onChange="generateimage(this.options[this.selectedIndex].value)">
<?php
while( $row = mysql_fetch_array($result))
{
echo "<option value=\"{$row[1]}/{$row[2]}\">{$row[0]}</option>\n";
}// $row[0] here is where random id part should go
?>
</select>
</td>
<td width="65%" valign="top" align="left"><ilayer id="dynamic1" width=100% height=178><layer id="dynamic2" width=100% height=178><div id="dynamic3"></div></layer></ilayer></td>
</tr>
</table>
</form>

<script>

//Dynamic Image selector Script- Dynamic Drive (www.dynamicdrive.com)
//For full source code, installation instructions,
//100's more DHTML scripts, visit dynamicdrive.com

var ie4=document.all
var ns6=document.getElementById
var tempobj=document.dynamicselector.dynamicselector2
if (ie4||ns6)
var contentobj=document.getElementById? document.getElementById("dynamic3"): document.all.dynamic3
function generateimage(which){
if (ie4||ns6){
contentobj.innerHTML='<center>Loading image...</center>'
contentobj.innerHTML='<center><img src="'+which+'"><br><br></center>'
}
else if (document.layers){
document.dynamic1.document.dynamic2.document.write('<center><img src="'+which+'"><br><br></center>')
document.dynamic1.document.dynamic2.document.close()
}
else
alert('You need NS 4+ or IE 4+ to view the images!')
}

function generatedefault(){
generateimage(tempobj.options[tempobj.options.selectedIndex].value)
}

if (ie4||ns6||document.layers){
if (tempobj.options.selectedIndex!=-1){
if (ns6)
generatedefault()
else
window.onload=generatedefault
}
}
</script><?php mysql_close($svrConn);?>

Twey
07-25-2006, 04:22 PM
Ah, I see. You want to just order the results randomly? Try adding ORDER BY RAND() to the end of your query.

jr_yeo
07-25-2006, 04:55 PM
okies :p thx :D

jr_yeo
03-26-2007, 09:43 PM
how do i now return results randomly in PostgreSQL? :p tried adding ORDER BY RAND() to the end of the query. returns an error "function rand() does not exist"

any ideas? :D

Twey
03-26-2007, 10:06 PM
In PostgreSQL the function is called RANDOM().