Tuesday 29 April 2008

making sure there's no blank listing on Random search

On the front page of my latest project at www.selfcateringcornwall.biz I have six boxes that advertise properties on the database randomly.

The main work horse of this code is in the mysql query that searches for a listing

[code]
$selectquery = "SELECT * FROM listings ORDER BY RAND() LIMIT 1";

[/code]
li.img1 > 0 AND

what i would like to do is make sure I am not displaying a blank listing - the following statement will check that two values and if they are ok then that row will be included in the random search.

[code]
$selectquery = "SELECT * FROM listings li WHERE li.img1 > 0 AND li.rent > 0 ORDER BY RAND() LIMIT 1";
[/code]

we can also add further filters - for example it may be cool just to list randomly some of the cheapest properties - this next search will only list properties that are under a value of £300

[code]
$selectquery = "SELECT * FROM listings li WHERE li.img1 > 0 AND li.rent > 0 AND li.rent <= 300 ORDER BY RAND() LIMIT 1";
[/code]

No comments: