したがって、テーブルの例では:
username ticket_number queue_time Doc 0 0 The 1st 3 not in sorted result Grumpy 0 0 Happy 0 0 Sleepy 1 111 The following are in sorted result Bashful 2 222 Sneezy 0 333 SnowWhite ??? 444 This is the current user (assign ???=4) Dopey 0 555 EvilQueen 0 666
ユーザー名SnowWhiteの列ticket_numberに値4を割り当てる方法。4は、最初の行のソートされた選択における行のランクです。
最初にソートされた結果を取得します:
$result = mysql_query("SELECT * FROM table WHERE [here the condition for your sorted array]");
// don't forget to remove the []. they don't go there.
while ($row = mysql_fetch_assoc($result))
{$array[] = $row;}
これにより、次のようになります:
[0] [username] [Sleepy]
[ticket_number] [1]
[queue_time] [111]
[1] [username] [Bashful]
[ticket_number] [2]
[queue_time] [222]
[2] [username] [Sneezy]
[ticket_number] [0]
[queue_time] [333]
[3] [username] [SnowWhite]
[ticket_number] [NULL]
[queue_time] [444]
次に、アレイを介してforeach
foreach ($array as $number => $row)
{if ($row[username] == $userinfo)
{$result = mysql_query('UPDATE table SET ticket_numer=' . ($number+1) . ' WHERE username=' . $userinfo);}}
ユーザー名SnowWhiteに対応する行は、ticket_numberとして($ number + 1)(3 + 1)を取得します