2011-12-15 66 views
0

代码工作正常,但我仍然希望在页面刷新时产生随机结果。现在代码给我的结果例如链接n.1,2,3,4,5 ...我不想他们是随机的例如没有。 3,9,1,...页面刷新时如何随机化标签云区域中的链接?

这里是代码:

<?php 


function print_cloud() 
{ global $use_ads_scrl; $res=""; if ($use_ads_scrl=="yes"){$res=print_cloud2();} return $res; } 

function print_cloud2() 
{ 

global $table_ads, $HTTP_GET_VARS; 

$city_sch=""; 
if ($HTTP_GET_VARS['city']!=""){$city_sch="and city='".$HTTP_GET_VARS['city']."' ";} 

$sql_query="select * from $table_ads where (adcommkey is null or adcommkey=0) and visible=1 $city_sch 
order by idnum desc limit 10"; 

$sql_res=mysql_query("$sql_query"); 

$min = '8'; // Minimum font size in pixel. 
$max = '22'; // Maximum font size in pixel. 
$decor = 'text-decoration:none;font-weight:100;'; // Inline CSS per link. 

$k1=""; $html_res=""; 
while ($row = mysql_fetch_array($sql_res)){ 
$k1="1"; 
if($row['adphotos']=='yes'){$check_ph=$photo_mark;} else {$check_ph="";} 

$html_res=$html_res." 
<a style=' ".$decor." font-size:".rand($min,$max)."px; font-family:tahoma,sans-serif; color:#3B5998;' href='index.php?md=details&id= ".$row['idnum']." '> ".$row['title']." </a> 
"; 


} 

$html_res=" 
$html_res 
"; 

if ($k1==""){$html_res="";} 

return $html_res; 
} 

?> 

回答

0

变化

order by idnum desc limit 10 

order by RAND() limit 10 

将从返回10个随机结果的表。

编辑:要从最新的X条目返回随机结果,您可以尝试子查询以返回最近的10个结果,然后随机排序该查询的结果。

SELECT * 
FROM (SELECT * FROM $table_ads 
     WHERE (adcommkey is null or adcommkey=0) 
     AND visible=1 
     ORDER BY idnum DESC LIMIT 10) `recent` 
ORDER BY RAND() 
+0

日本鬼子,已经这样做了,但你没得到最后10添加的链接,但是从所有数据库:( 我想采取随机从去年10添加链接,所以可以说从随机采取随机当天的新闻 – user1050689 2011-12-15 17:43:42