darky_miguel Posted May 25, 2008 at 11:12 AM Report Share #187322 Posted May 25, 2008 at 11:12 AM pessoal eu quero ir a base de dados buscar 10 rows aleatoriamente eu fiz assim if ($accao=="aoacaso") { $sql="SELECT * FROM filmes"; $result=mysql_query($sql); while($row=mysql_fetch_assoc($result)) { $ids[]=$row['cod_filme']; } for ($i=0;$i<11;$i++) { $numerorand=mt_rand(0, 10); $final[]=$ids[$numerorand]; } print_r($final); } mas isto tem bug pk me returna rows repetidos e eu nao queria essa situaçao podem me ajudar ? Link to comment Share on other sites More sharing options...
Hipnoted Posted May 25, 2008 at 01:15 PM Report Share #187339 Posted May 25, 2008 at 01:15 PM Usa a cláusula DISTINCT no sql... assim elimina-te os duplicados. "Nunca discutas com um idiota. Eles arrastam-te até ao seu nível e depois ganham-te em experiência" Link to comment Share on other sites More sharing options...
darky_miguel Posted May 25, 2008 at 01:23 PM Author Report Share #187343 Posted May 25, 2008 at 01:23 PM os duplicados nao vem do sql vem do for for ($i=0;$i<11;$i++) { $numerorand=mt_rand(0, 10); $final[]=$ids[$numerorand]; } mas nao sei como fazer para o rand nao me gerar numeros iguais Link to comment Share on other sites More sharing options...
zero-cool Posted May 25, 2008 at 05:04 PM Report Share #187393 Posted May 25, 2008 at 05:04 PM $sql = "SELECT * FROM abc ORDER BY RAND() LIMIT 100"; Link to comment Share on other sites More sharing options...
darky_miguel Posted May 25, 2008 at 05:06 PM Author Report Share #187394 Posted May 25, 2008 at 05:06 PM obrigado funionou mas eu ja arranjei uma alternativa eu ja meto aqui Link to comment Share on other sites More sharing options...
darky_miguel Posted May 25, 2008 at 05:08 PM Author Report Share #187395 Posted May 25, 2008 at 05:08 PM function aleatorio($min,$max,$count) { $new_array=array(); //tratamento de erros se o count ultrapassar o intervalo if($count>=($max-$min)) { $count=$max-$min; } for($i=0;$i<=$count;$i++) { $num=mt_rand($min,$max); while(in_array($num,$new_array)) { $num=mt_rand($min,$max); } $new_array[$i]=$num; } esta funçao foi um colega meu que fez e eu adaptei para mim if ($accao=="aoacaso") { $sql="SELECT * FROM filmes where visivel = 1"; $result=mysql_query($sql); while($row=mysql_fetch_assoc($result)) { $ids[]=$row['cod_filme']; } $var=aleatorio(1,10,12); for ($i=0;$i<12;$i++) { $final[]=$ids[$var[$i]]; } ?> <table border="0"> <tr> <?php $i=0; foreach ($final as $linha) { echo "<td>"; if (!nome_filme($linha)=="") { $i++; echo nome_filme($linha); echo "<br>"; ?> <img src="<?php echo substr(capa_filme($linha),3); ?>" width="90" height="140" /> <?php echo "</td>"; } if($i%6==0) { echo "<tr><td coslpan=5> </td></tr></tr><tr>"; } } ?> </tr></table> <?php } Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now