Jump to content

How to get random number based on the condition.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
faith89

faith89

    Newbie

  • Members
  • PipPip
  • 14 posts
i have two table in my database which is the room table and roomtype table.i'm using phpmyadmin.

room table
room_no
r_roomtypeID


roomtype table
roomtypeID
roomtype


how can i create a query that can select A random room_no from "room table "
based on the roomtypeID in the "table roomtype"

p/s - each roomtypeID got it's own quantity room no.

roomtypeID room_no

example : Single room : 1-10
: Deluxe room : 11-20
: Suite room : 21-30

right now i'm only having this kind of idea. the random number from room. I don't know how can i generate A random room_no from table room based on the roomtypeID.

(SELECT room_no FROM room ORDER BY RAND( )LIMIT 1) 


any ideas is really appreciated:)

#2
Arctic Fire

Arctic Fire

    Learning Programmer

  • Members
  • PipPipPip
  • 48 posts
I'm not sure if I'm understanding correctly. Are you talking about this?

SELECT * FROM room WHERE r_roomtypeID = '7' ORDER BY rand() LIMIT 1

Just a note, ORDER BY rand() is highly inefficient and you should avoid it at all costs in a production environment. Instead, do something like this:

$result = mysql_query("SELECT * FROM room WHERE r_roomtypeID = '7'");
if(mysql_num_rows($result) > 0){
	$random = rand(0,mysql_num_rows($result) - 1);
	mysql_data_seek($random);
	$roomInfo = mysql_fetch_assoc($result);
	
	print_r($roomInfo);
}


#3
faith89

faith89

    Newbie

  • Members
  • PipPip
  • 14 posts
hurm..Arctic Fire , can you please explain this part

Quote

$random = rand(0,mysql_num_rows($result) - 1);
.

i try using code you've given, and try to echo the value. And, there is value return But there's error saying that :

Warning: Wrong parameter count for mysql_data_seek() in C:\xampp\htdocs\modifyAPOS\Lastly.php on line 19
Array ( [room_no] => 11 ) 


#4
faith89

faith89

    Newbie

  • Members
  • PipPip
  • 14 posts
Arctic Fire,actually...can you please explain your codes because i get the error after try to modify you codes. TQ:P

#5
Arctic Fire

Arctic Fire

    Learning Programmer

  • Members
  • PipPipPip
  • 48 posts
Woops, I didn't notice that error.

Change this:

mysql_data_seek($random);

to this:

mysql_data_seek($result,$random);

#6
faith89

faith89

    Newbie

  • Members
  • PipPip
  • 14 posts
ok..tq for the reply..
i already successfuly solved this problem..:)