uses the function uniqueid
PHP, serial something like:
$dados2 = connection::select('SELECT * FROM tab1');
$contagem=0;
foreach ($dados2 as $reg) {
$campo1 = $reg['campo1'];
$campo2 = $reg['campo2'];
$contagem++;
}
$sequencia_aleatoria=rand(0,$contagem);
connection::exec("insert into tab2(campo1,campo2,sequencia_aleatoria) values('$campo1','$campo2','$sequencia_aleatoria')");
the function uniqid()
generates a hash based on milliseconds so there is no possibility of repetition.
I guess your code would be like this:
$dados2 = connection::select('SELECT * FROM tab1');
$numero=count(dados2);
foreach ($dados2 as $reg) {
$campo1 = $reg['campo1'];
$campo2 = $reg['campo2'];
$sequencia_aleatoria=rand(0,$numero);
connection::exec("insert into tab2(campo1,campo2,sequencia_aleatoria) values('$campo1','$campo2','$sequencia_aleatoria')");
unset($sequencia_aleatoria);
}
Rotate this q works
<?php
$contagem=420; // aqui iioria o count($resultado);
for($i=0;$i<=$contagem;$i++){
echo rand(0,$contagem)."<br>";
}
?>
logica he the same and ta rolando.
forehead online here
http://phptester.net/
instead of you do a foreach do a for
$dados2 = connection::select('SELECT * FROM tab1');
$numero=count(dados2);
for($i=0;$i<$numero;i++) {
$reg=$dados2[$i];
$campo1 = $reg['campo1'];
$campo2 = $reg['campo2'];
$sequencia_aleatoria=rand(0,$numero);
connection::exec("insert into tab2(campo1,campo2,sequencia_aleatoria) values('$campo1','$campo2','$sequencia_aleatoria')");
unset($sequencia_aleatoria);
}
From what I understand you want to fill an Array with numbers from 0 to 100 in random order ? Records cannot be entered in order and you shuffle the output ?
– Lucas Queiroz Ribeiro
Exact @Ucas. Type beyond the autoincremet of Insert, I want to have another field, so I can change this sequence when you want.
– Eduardo Santos
But it has to be random.
– Eduardo Santos
it has to contain all the numbers from 0 to the right total number of records ? So it’s not random, it has a defined sequence but it’s assorted. You can popular an array from 0 to 100 for example, and then do a Sort
– Lucas Queiroz Ribeiro
The second example @stderr does just that, I think it’s right
– Lucas Queiroz Ribeiro