Jump to content

Recommended Posts

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

<?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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.