darky_miguel Posted May 25, 2008 Report Share Posted May 25, 2008 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 Report Share Posted May 25, 2008 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 Author Report Share Posted May 25, 2008 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 Report Share Posted May 25, 2008 $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 Author Report Share Posted May 25, 2008 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 Author Report Share Posted May 25, 2008 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