从 while 循环构建一个输出数组,然后计算其中的值的数量。使用 count(array) 使用 rand() 计算随机数,然后迭代输出数组,并使用 if 语句判断索引何时与随机数匹配。
<?php
mysql_connect("localhost","root","");
mysql_select_db("bravo");
$res=mysql_query("select * from coisas");
?>
<div>
<?php
$output_array = array();
while ($row=mysql_fetch_array($res)) {
$output_array[] = "<img src=\"{$row['imagem']}\">";
}
//If you want the image to appear closer to middle, use fractions of $output_array
//EG: rand(count($output_array)/3, count($output_array)*2/3));
$rand_key = rand(0, count($output_array)-1);
foreach ($output_array as $key => $img) {
if ($key == $rand_key) {
//echo your adsense code
}
echo $img;
}
?>
</div>