Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Randomise Results

Status
Not open for further replies.

mrblonde100

Technical User
Nov 22, 2009
5
GB
Database Version: 5.0.45

Hi,

I'm having trouble in getting a random order to my SQL query.

I have read posts about the RAND() LIMIT but i can’t get it to work. It seems to break the echo and there appears to be a consensus that RAND() isn’t the best option anyway.
I’m trying to get the string in the $result variable to randomise the record order when echoed.

At the moment the code below is working perfectly, but the order is always static depending on what I ORDER BY.
I’m a real PHP beginner but managed to cobble together the basics of what i need (with the help of this forum), but this random issue is baffling me! I don’t want the elements that make up fetched record like image1, state etc, randomised, but the records themselves. Sorry for my layman’s terms, I don’t yet have the PHP vocabulary to properly express myself I suppose.

I’m basically pushing the end result to Flash so it can dynamically populate the various fields within Flash.
I do hope someone can shine some light on the issue.

Many thanks

Code:
<?php
$connect = mysql_connect("HOST", "USERNAME", "PASSWORD");
mysql_select_db("DATABASE");
$result = mysql_query("SELECT price, id, image1, state, locality, listdate, featured, sold FROM table WHERE state = 'berkshire' AND image1 > '' AND featured = '1' AND sold <= '2' ORDER BY listdate DESC");
$intro = mysql_query("SELECT introtext FROM table WHERE catid = 929 AND ordering = 2");
$cant = 0;
while($row=mysql_fetch_array($intro)){
	$introstripped = "introtext$cant=$row[introtext]&";
	$introstripped = strip_tags ($introstripped);
	echo $introstripped;	
}
while($row=mysql_fetch_array($result)){
	echo "locality$cant=$row[locality]&price$cant=$row[price]&propid$cant=$row[id]&image1$cant=$row[image1]&area$cant=$row[state]&";
$cant++;	
}
echo "cant=$cant";
?>
 
basically you restructure your query as follows

Code:
select * from mytable where column=value order by rand()
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top