Daniele Cruciani
Programmatore Developer PHP/MySQL Freelance
mobile: +39 3489215204

icq skype msn linkedin fb t ff youtube picasa google me on github

Advertisement

You are here

shuffle db table order

    simple code to shuffle a table ... with php shuffle:

    $result = $mysql->query("SELECT id FROM $table ORDER BY id");
    $rows = $result->num_rows;
    $order = shuffle(range(1,$rows));
    while($row=$result->fetch_row()) {
      $num=array_shift($order);
      $mysql->query("UPDATE $table SET rand_sort=$num WHERE id=".$row[0]);
    }
    

    added:
    with MySQL RAND():

    $result = $mysql->query("SELECT id FROM $table ORDER BY RAND()");
    for($i=1;$row=$result->fetch_row();$i++) {
      $mysql->query("UPDATE $table SET rand_sort=$i WHERE id=".$row[0]);
    }
    

    comments?

    Tags: