mrblonde Posted November 27, 2009 Share Posted November 27, 2009 Database Version: 5.0.45 Hi, I'm having trouble in getting a random order to my SQL query. I know there is post already explaining a way of achieving this and I don’t mean to annoy anyone, but I didn’t understand it. Not the authors fault but my lack or PHP experience. 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 have also read about how to implement an alternative, but in truth i don’t really understand how i would implement it with my code. http://www.phpfreaks.com/forums/index.php/topic,125759.0.html 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, 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 <?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"; ?> Link to comment https://forums.phpfreaks.com/topic/183142-randomising-echo-results/ Share on other sites More sharing options...
mrblonde Posted November 27, 2009 Author Share Posted November 27, 2009 I'm sorry i posted this twice... I didnt realise the original thread had gone through. Link to comment https://forums.phpfreaks.com/topic/183142-randomising-echo-results/#findComment-966530 Share on other sites More sharing options...
abazoskib Posted November 27, 2009 Share Posted November 27, 2009 <?php $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 RAND()"); ?> RAND() will become less and less efficient as more rows are returned so be careful with its use. When that happens, you'll need a different method. Link to comment https://forums.phpfreaks.com/topic/183142-randomising-echo-results/#findComment-966548 Share on other sites More sharing options...
mrblonde Posted November 27, 2009 Author Share Posted November 27, 2009 Hi thanks for the reply and advice. I think Rand will suffice for now. I tried the code you recommended. $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 RAND()"); I had tried this before but hoped i had missed something. But when run it pumps out the following message. Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/fhlinux136/n/norwegianproperty.net/user/htdocs/get/hedmark-props.php on line 25 Line 25 is about where the while loop kicking in to return the results string. Link to comment https://forums.phpfreaks.com/topic/183142-randomising-echo-results/#findComment-966560 Share on other sites More sharing options...
mrblonde Posted November 27, 2009 Author Share Posted November 27, 2009 No sorry it works thanks! I re tried your suggestion and it went through. thanks Link to comment https://forums.phpfreaks.com/topic/183142-randomising-echo-results/#findComment-966569 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.