Jump to content

resultados aleatorios


darky_miguel
 Share

Recommended Posts

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.