views:

71

answers:

4

I have a online PHP system where users vote different awards for their friends, however I am finding that the awards in the middle of the page and at the bottom get less votes overall. I would prefer this to be evenly distributed so came up with ordering the list of awards by random to make it different each time you load the page.

This however seems to confuse users as then they save or revisit the page everything moves, is there a way that I can order the list randomly but save this order for that user, meaning it's different for each user.

The list of votes comes from a database and the names of the awards are preset.

Do you know a way to do this?

In the end I used:

  //Shuffle & Organise
    if(is_numeric($pg)) { $start = ($pg*15)-14; $end = $pg*15; $pg = (int) $pg; } else { $start = 1; $end = 15; $pg = (int) 1; }
    if($end>count($vote_name)) { $end = count($vote_name); }
    $vote_boxes = range($start,$end);
    srand($user['id']); 
    shuffle($vote_boxes);

  //Create the voting boxes + js
    foreach($vote_boxes as $row) {
     $content .= vote_form($row);
    }
+2  A: 

If database table isn't very large (because this will melt your database if you're talking about more than a few rows).

<?PHP
$ip = $_SERVER['REMOTE_ADDR'];
$sql = "SELECT * FROM table ORDER BY md5(CONCAT(id,'$ip');";
$result = db_query($sql);
?>

That will hash the id column (pick any column, really) of the record and the client's IP address. Every IP address that visits the site will get a different ordering, but it will remain static as long as their IP does.

timdev
A: 

To do the actual shuffling use Fisher-Yates shuffle algorithm. use something unique to each user to seed the prng before the shuffle.

jk
+3  A: 

i suppose your users have unique ids in the users table, so why not simply

 select * from awards order by rand($user_id)

rand() can accept an argument that means 'seed'

stereofrog
I did not know it accepted a seed, always wondered why it had ()'sVery useful but not exactly what I am looking for this time.
Pez Cuckow
This is _exactly_ what you were looking for.
ntd
+1  A: 

The most efficient way probably is to create a completely random array order and cache that for every user.

Except for the MySQL sollution postet above, you may use php in a very similar style: You can use the session id as a start for the random number generator and than shuffle the array using that "random" number. By doing that, every user recieves a differently sorted list, whenever he requests your site (at least as long as the session does not expire).

<?php

  $array = array("Cat", "Dog", "Mouse");

  session_start();
  $session_id_int = (int)session_id();
  srand($session_id_int);

  echo '<pre>BEFORE:'.PHP_EOL;
  print_r($array);

  shuffle($array);

  echo 'AFTER:'.PHP_EOL;
  print_r($array);
FlorianH
I used a solution that was based on the information you posted combined with the above MySQL solution. I will post it here when it's finished.
Pez Cuckow