Results 1 to 9 of 9

Thread: random sql

  1. #1
    Join Date
    Apr 2006
    Posts
    429
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool random sql

    how to randomize unique id from table?

    i have id, path, file as field names

    i want something like
    HTML Code:
    <option value="path/file">[I]random id[/I]</option>
    Please don't mind me. I am just posting a lot of nonsense.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I don't quite understand. Do you want to select a random existing ID from the table, or create a new one?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Apr 2006
    Posts
    429
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey
    Do you want to select a random existing ID from the table
    sorry existing random id from table
    Please don't mind me. I am just posting a lot of nonsense.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    $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))];
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Apr 2006
    Posts
    429
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    sorry but where will i put it?
    Code:
    <?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);?>
    Please don't mind me. I am just posting a lot of nonsense.

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Ah, I see. You want to just order the results randomly? Try adding ORDER BY RAND() to the end of your query.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Apr 2006
    Posts
    429
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    okies thx
    Please don't mind me. I am just posting a lot of nonsense.

  8. #8
    Join Date
    Apr 2006
    Posts
    429
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default follow - up question

    how do i now return results randomly in PostgreSQL? tried adding ORDER BY RAND() to the end of the query. returns an error "function rand() does not exist"

    any ideas?
    Please don't mind me. I am just posting a lot of nonsense.

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    In PostgreSQL the function is called RANDOM().
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •