toolman Posted February 8, 2010 Share Posted February 8, 2010 Hi, I have this code which echos the latest 2 records from a database. However, I want to display 2 records at random. Can anyone tell me how to do this? Thanks. This is my code: <?php $sql = "SELECT a.*, UNIX_TIMESTAMP(a.createdon) AS timestamp, SIGN(feat.featuredtill-NOW()) AS isfeat, COUNT(*) AS piccount, p.adid AS haspics, scat.subcatname, scat.catid, cat.catname FROM $t_ads a INNER JOIN $t_cities ct ON a.cityid = ct.cityid INNER JOIN $t_subcats scat ON a.subcatid = scat.subcatid INNER JOIN $t_cats cat ON scat.catid = cat.catid LEFT OUTER JOIN $t_adpics p ON a.adid = p.adid AND p.isevent = '0' LEFT OUTER JOIN $t_featured feat ON a.adid = feat.adid AND feat.adtype = 'A' WHERE SIGN(feat.featuredtill-NOW()) = 1 AND $visibility_condn $loc_condn GROUP BY a.adid ORDER BY a.createdon DESC LIMIT 2"; $res_latest = mysql_query($sql) or die($sql.mysql_error()); while($row = mysql_fetch_array($res_latest)) { if($row['isfeat']) { $catname_inurl = RemoveBadURLChars($row['catname']); $subcatname_inurl = RemoveBadURLChars($row['subcatname']); if($sef_urls) $url = "{$vbasedir}$xcityid/posts/{$row[catid]}_{$catname_inurl}/{$row[subcatid]}_{$subcatname_inurl}/$row[adid]_" . RemoveBadURLChars($row['adtitle']) . ".html"; else $url = "?view=showad&adid=$row[adid]&cityid=$xcityid&lang=$xlang{$link_extra}"; ?> <?php $feat_class = "class=\"featured\""; $feat_img = "<img src=\"images/featured.gif\" align=\"absmiddle\">"; ?> <a href="<?php echo $url; ?>" class="featured_top"><?php echo $row['adtitle']; ?></a> <?php } } ?> Link to comment https://forums.phpfreaks.com/topic/191361-help-displaying-random/ Share on other sites More sharing options...
jl5501 Posted February 8, 2010 Share Posted February 8, 2010 order by rand() limit 2 Link to comment https://forums.phpfreaks.com/topic/191361-help-displaying-random/#findComment-1008872 Share on other sites More sharing options...
toolman Posted February 8, 2010 Author Share Posted February 8, 2010 Thanks very much! Link to comment https://forums.phpfreaks.com/topic/191361-help-displaying-random/#findComment-1008879 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.