Current location: Hot Scripts Forums » Programming Languages » PHP » [SOLVED] Random Id From table


Random Id From table

Closed Thread
  #1 (permalink)  
Old 11-19-09, 06:12 PM
smithygotlost smithygotlost is offline
Aspiring Coder
 
Join Date: Jul 2006
Location: United Kingdom
Posts: 412
Thanks: 12
Thanked 3 Times in 3 Posts
Random Id From table

Heya guys

ok i have a table in my db called room under the table i have roomid and z the roomid can be anything from 1 - 1000 but the z will be the same if its the same area. now what i wanna do is select from roomid where id=300 that will then give the z from that table in this case 3

so right now i have z=3 ,what i now wanna do is get all the roomid's where z=3 and choose a random one, and display it as a variable

How can i do that ?

Cheers
Mike
__________________
Make People Friendly Say " Thanks "
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
  #2 (permalink)  
Old 11-19-09, 06:49 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
PHP Code:

mysql_query('SELECT COUNT(*) AS \'count\' FROM `room` WHERE `z`=3');

$rResult=mysql_fetch_assoc();
$iNumRooms=$rResult['count'];
$iRandom=rand(1,$iNumRooms);
mysql_query('SELECT * FROM `room` WHERE `z`=3 LIMIT '.$iRandom.',1');
$rResult=mysql_fetch_assoc(); 
Not tested.

Basic idea - count the number of z=3s, generate a random number within that range, use that number as an offset to select the data.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
The Following User Says Thank You to wirehopper For This Useful Post:
smithygotlost (11-20-09)
  #3 (permalink)  
Old 11-20-09, 04:43 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
This should work too:
PHP Code:

$id=300;
$sql mysql_query("SELECT z FROM room WHERE roomid='$id'");
$r mysql_fetch_row($sql);

$sql mysql_query("SELECT * FROM room WHERE z='$r[0]' ORDER BY RAND()");
while(
$row mysql_fetch_assoc($sql))
{
 if(
$row["roomid"] != $id)
 {
  
$roomid $row["roomid"];
  
$z $row["z"];
  break;
  }
 }
echo 
$roomid."<br />".$z."<br />"
__________________
Jerry Broughton

Last edited by job0107; 11-20-09 at 05:43 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
The Following User Says Thank You to job0107 For This Useful Post:
smithygotlost (11-20-09)
  #4 (permalink)  
Old 11-20-09, 06:19 AM
smithygotlost smithygotlost is offline
Aspiring Coder
 
Join Date: Jul 2006
Location: United Kingdom
Posts: 412
Thanks: 12
Thanked 3 Times in 3 Posts
Very Nice Guys

ok ive moded Jerry's into this

PHP Code:

//Move Mobs
$mob mysql_query("Select * from `roommobs` where `move`='1'")or die(mysql_error());
$mobid mysql_result($mob,0"mobid");
$id mysql_result($mob,0"roomid");
$sql mysql_query("SELECT z FROM room WHERE roomid='$id'");
$r mysql_fetch_row($sql);

$sql mysql_query("SELECT * FROM room WHERE z='$r[0]' ORDER BY RAND()");
while(
$row mysql_fetch_assoc($sql))
{
 if(
$row["roomid"] != $id)
 {
  
$roomid $row["roomid"];
  
$z $row["z"];
  break;
  }
 }
mysql_query("update `roommobs` set `roomid`='$roomid' where `mobid`='$mobid' and `move`='1'"); 
Nice bit of code btw erm next quest if i have more than 1 mob how do i make it loop till its done them all ? would it be something like a FOR ?

Cheers
Mike
__________________
Make People Friendly Say " Thanks "
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
  #5 (permalink)  
Old 11-20-09, 06:56 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Maybe like this:
PHP Code:

$mob mysql_query("Select * from `roommobs` where `move`='1'")or die(mysql_error());
while(
$rows mysql_fetch_assoc($mob))
{
 
$mobid $rows["mobid"];
 
$id $rows["roomid"];
 
$sql mysql_query("SELECT z FROM room WHERE roomid='$id'");
 
$r mysql_fetch_row($sql);

 
$sql mysql_query("SELECT * FROM room WHERE z='$r[0]' ORDER BY RAND()");
 while(
$row mysql_fetch_assoc($sql))
 {
  if(
$row["roomid"] != $id)
  {
   
$roomid $row["roomid"];
   
$z $row["z"];
   break;
   }
  }
 
mysql_query("update `roommobs` set `roomid`='$roomid' where `mobid`='$mobid' and `move`='1'");
 } 
__________________
Jerry Broughton
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
The Following User Says Thank You to job0107 For This Useful Post:
smithygotlost (11-20-09)
  #6 (permalink)  
Old 11-20-09, 06:58 AM
smithygotlost smithygotlost is offline
Aspiring Coder
 
Join Date: Jul 2006
Location: United Kingdom
Posts: 412
Thanks: 12
Thanked 3 Times in 3 Posts
Never dissapoint do you jerry

Cheers
Fella
__________________
Make People Friendly Say " Thanks "
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
  #7 (permalink)  
Old 11-20-09, 07:15 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Your welcome.
__________________
Jerry Broughton
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Closed Thread

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
MYSQL database countll Database 2 06-19-07 05:20 PM
What do you think of my database structure? Oskare100 Database 5 12-27-06 08:43 AM
Pretty simple ( i think) question any help!?!? 0o0o0 PHP 2 04-07-06 11:47 AM
Random or Sequential Table Content! johnmon11 Script Requests 1 03-09-06 11:33 AM
picking random entries with a filter... Double selection problem dsumpter PHP 7 11-16-03 08:19 PM


All times are GMT -5. The time now is 08:26 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.