php - Select a user according to set probability -
my database stores info users, groups, , relationships. 1 of columns, fcount
, in users table tracks number of relationships each user has had within current group; starts @ 0, , increment when appropriate.
need write script selects users in given group , randomly selects 1 of them probability of being selected being based on number of relationships 1 has had; fewer relationships means greater probability.
currently, accomplish this, minus probability part, code:
$query = "select uid users groupid=:gid , status='1'"; ... while ($row = $stmt->fetch(pdo::fetch_num)) { $friends[] = $row[0]; } foreach ($users $value) { $key = array_search($value, $friends); unset($friends[$key]); shuffle($friends); $newrel = end($friends); $user = $value; $friends[] = $value; $query = "update users set rel=:newrel uid=:uid"; $query_params = array(':newrel' => $newrel, ':uid' => $user ); ... }
i thinking easiest way adjust probability edit $friends
array users show more once. so, users fcount
of 0 repeated in array 5 times, , users fcount
of 1 repeated 3 times. [maybe isn't best way handle it, makes sense me , fits code have. you're free offer better scheme.]
what haven't managed figure out how take array of users , multiply entries of users ought multiplied.
select uid, fcount users groupid=:gid , status='1';
returns array reflective of table:
+-----+--------+ | uid | fcount | +-----+--------+ | 105 | 3 | | 106 | 2 | | 107 | 0 | | 108 | 0 | | 109 | 1 | +-----+--------+
which turns array this:
array(15) { [0]=> string(3) "105" [1]=> string(3) "106" [2]=> string(3) "107" [3]=> string(3) "107" [4]=> string(3) "107" [5]=> string(3) "107" [6]=> string(3) "107" [7]=> string(3) "108" [8]=> string(3) "108" [9]=> string(3) "108" [10]=> string(3) "108" [11]=> string(3) "108" [12]=> string(3) "109" [13]=> string(3) "109" [14]=> string(3) "109" }
it seems me accomplished foreach
pushes each userid n times new array according if statements in loop. i'll try construct it, though i'm fail.
$problist = array(); foreach ($row $value) { if ($value['fcount'] == 0) { array_push($problist, $value['uid'], $value['uid'], $value['uid'], $value['uid'], $value['uid']); } elseif ($value['fcount'] == 1) { array_push($problist, $value['uid'], $value['uid'], $value['uid']); } elseif ($value['fcount'] >= 2) { $problist[] = $value['uid']; } }
Comments
Post a Comment