sidrs Posted March 6, 2010 Report Share Posted March 6, 2010 sveiki! vaidzētu padomu, kādā veidā iespējams updeitot useru tabulā (piemēram 5 useriem) katram random skaitli (lai katram būtu savādāks nevis visi vienādi),vienā lapas ielādē. vai ir kādi varianti? Quote Link to comment Share on other sites More sharing options...
waplet Posted March 6, 2010 Report Share Posted March 6, 2010 ar ciklu Quote Link to comment Share on other sites More sharing options...
briedis Posted March 6, 2010 Report Share Posted March 6, 2010 atlasa masīvā visus lietotāju id Tad iet cauri masīvam un veic update kvērijus katram lietotājam uzstādot random skaitli... Tik tā būs relatīvi laikietilpīga darbība, to nevajadzētu veikt parāk bieži... Quote Link to comment Share on other sites More sharing options...
Gints Plivna Posted March 6, 2010 Report Share Posted March 6, 2010 hmmm kāpēc ciklu? Man būs 4 useri ar idiem 1, 2, 3, 4 vienā piegājienā laboju 2 un 4. mysql> create table users (id integer, rnd integer); Query OK, 0 rows affected (0.16 sec) mysql> insert into users values (1, null); Query OK, 1 row affected (0.03 sec) mysql> insert into users values (2, null); Query OK, 1 row affected (0.03 sec) mysql> insert into users values (3, null); Query OK, 1 row affected (0.05 sec) mysql> insert into users values (4, null); Query OK, 1 row affected (0.03 sec) mysql> update users set rnd = rand()*10000 -> where id in (2, 4); Query OK, 2 rows affected (0.05 sec) Rows matched: 2 Changed: 2 Warnings: 0 mysql> select * from users; +------+------+ | id | rnd | +------+------+ | 1 | NULL | | 2 | 841 | | 3 | NULL | | 4 | 7387 | +------+------+ 4 rows in set (0.00 sec) Gints Plivna http://datubazes.wordpress.com/ Quote Link to comment Share on other sites More sharing options...
chizijs Posted March 6, 2010 Report Share Posted March 6, 2010 $rand1 = rand(1,5); $rand2 = rand(1,5); $rand3 = rand(1,5); $rand4 = rand(1,5); $rand5 = rand(1,5); mysql_query("UPDATE users SET rand_skaitlis = '".mysql_real_escape_string($rand1)."' WHERE id = '".mysql_real_escape_string($rand1)."'") or die(mysql_error()); mysql_query("UPDATE users SET rand_skaitlis = '".mysql_real_escape_string($rand2)."' WHERE id = '".mysql_real_escape_string($rand2)."'") or die(mysql_error()); mysql_query("UPDATE users SET rand_skaitlis = '".mysql_real_escape_string($rand3)."' WHERE id = '".mysql_real_escape_string($rand3)."'") or die(mysql_error()); mysql_query("UPDATE users SET rand_skaitlis = '".mysql_real_escape_string($rand4)."' WHERE id = '".mysql_real_escape_string($rand4)."'") or die(mysql_error()); mysql_query("UPDATE users SET rand_skaitlis = '".mysql_real_escape_string($rand5)."' WHERE id = '".mysql_real_escape_string($rand5)."'") or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
2easy Posted March 7, 2010 Report Share Posted March 7, 2010 (edited) njaa chizij, no kura laika skaitļus liek pēdiņās un vēl eskeipo ar mysql_real_escape_string()??? :D:D:D Gints Plivna +1 taču ja pieciem useriem vajag updeitot 5 nelielus random skaitļus, teiksim 1..5, tad jau gan nāksies rakstīt ar php 5 kverijus turklāt ņemt secīgus skaitļus ārā no masīva ar array_rand(), jo pie tik maza random skaitļu intervāla ir liela varbūtītba, ka tie atkārtosies piemēram, pēc chizija (latviski laikam būtu "sieriņš", ja tas ir latviskots angļu cheese?) varianta man nupat sanāca 2, 4, 4, 4, 4 (labi, ka neatkārtojas :D:D:D) Edited March 7, 2010 by 2easy Quote Link to comment Share on other sites More sharing options...
Gints Plivna Posted March 7, 2010 Report Share Posted March 7, 2010 taču ja pieciem useriem vajag updeitot 5 nelielus random skaitļus, teiksim 1..5, tad jau gan nāksies rakstīt ar php 5 kverijus turklāt ņemt secīgus skaitļus ārā no masīva ar array_rand(), jo pie tik maza random skaitļu intervāla ir liela varbūtītba, ka tie atkārtosies Think out of the box (php) :P Lietojam to pašu tabulu users. Taisu updeitu 2 reizes arī atlasot datus, lai pierādītu, ka ir random (cik nu labs ir random ģenerētājs mysqlā iebūvēts) un rezultāti nesakrīt. mysql> set @i = 0; Query OK, 0 rows affected (0.00 sec) mysql> update users set rnd = (@i := @i + 1) -> order by rand(); Query OK, 4 rows affected (0.03 sec) Rows matched: 4 Changed: 4 Warnings: 0 mysql> select * from users; +------+------+ | id | rnd | +------+------+ | 1 | 3 | | 2 | 1 | | 3 | 2 | | 4 | 4 | +------+------+ 4 rows in set (0.00 sec) mysql> set @i = 0; Query OK, 0 rows affected (0.00 sec) mysql> update users set rnd = (@i := @i + 1) -> order by rand(); Query OK, 4 rows affected (0.03 sec) Rows matched: 4 Changed: 4 Warnings: 0 mysql> select * from users; +------+------+ | id | rnd | +------+------+ | 1 | 1 | | 2 | 4 | | 3 | 3 | | 4 | 2 | +------+------+ 4 rows in set (0.00 sec) Gints Plivna http://datubazes.wordpress.com Quote Link to comment Share on other sites More sharing options...
2easy Posted March 7, 2010 Report Share Posted March 7, 2010 Gints Plivna +1000 :)) oo šis ir smuks mysql variabļu pielietojums!!! turklāt vēl tādā kombinācijā ar ORDER BY RAND() vnk woooow good job! ;) P.S. protams, ir smuki, ka šāds daudzu rindu update ir noslēpts vienā kverijā, taču tehniski no performances viedokļa tas varētu būt tikai nedaudz ātrāk kā pēc indeksēta id apdeitot katru useri atsevišķi. bet risinājums anyway ir loti elegants! Quote Link to comment Share on other sites More sharing options...
sidrs Posted March 7, 2010 Author Report Share Posted March 7, 2010 Esu iesacējs un daudzkas ir tumša bilde :) bet nu kautko sagrābstot piespiedu updeitot katram random ciparu,tik nezinu cik tas ir pareizi :) $result = mysql_query("SELECT id FROM users where atlase='1'"); while ($row = mysql_fetch_row($result)) { $user1=$row[0]; $user2=$row[1]; $user3=$row[2]; $user4=$row[3]; $user5=$row[4]; $user6=$row[5]; $rand1=rand(1,100); $rand2=rand(1,100); $rand3=rand(1,100); $rand4=rand(1,100); $rand5=rand(1,100); $rand6=rand(1,100); mysql_query("UPDATE users SET aile=$rand1 WHERE id='$user1'"); mysql_query("UPDATE users SET aile=$rand2 WHERE id='$user2'"); mysql_query("UPDATE users SET aile=$rand3 WHERE id='$user3'"); mysql_query("UPDATE users SET aile=$rand4 WHERE id='$user4'"); mysql_query("UPDATE users SET aile=$rand5 WHERE id='$user5'"); mysql_query("UPDATE users SET aile=$rand6 WHERE id='$user6'"); } kritiku studijā :D Quote Link to comment Share on other sites More sharing options...
waplet Posted March 7, 2010 Report Share Posted March 7, 2010 es saku, ka neies. Quote Link to comment Share on other sites More sharing options...
briedis Posted March 7, 2010 Report Share Posted March 7, 2010 (edited) es saku, ka neies. Moš viņam users tabulas struktūra ir ar kolonnām: id | id2 | id3 | id4 | id5 | id6 | aile | atlase Ko tad teiksi? :) Edited March 7, 2010 by briedis Quote Link to comment Share on other sites More sharing options...
waplet Posted March 7, 2010 Report Share Posted March 7, 2010 nu labi pieņemsim.. viņš 6 useriem pa 6 reizēm samainīš id Quote Link to comment Share on other sites More sharing options...
briedis Posted March 7, 2010 Report Share Posted March 7, 2010 nu labi pieņemsim.. viņš 6 useriem pa 6 reizēm samainīš id Nu jā, tas, protams, būs neloģiski, bet strādāt ta strādās :) Quote Link to comment Share on other sites More sharing options...
sidrs Posted March 7, 2010 Author Report Share Posted March 7, 2010 cik sapratu tad šitie nosaka kads userim id $user1=$row[0]; $user2=$row[1]; $user3=$row[2]; $user4=$row[3]; $user5=$row[4]; $user6=$row[5]; un tad pec id liek random skaitļus iekša.. id kolona ir viena un tur nekas nav mainijies un skaitli aile ielikas katram cits.. Quote Link to comment Share on other sites More sharing options...
briedis Posted March 7, 2010 Report Share Posted March 7, 2010 cik sapratu tad šitie nosaka kads userim id $user1=$row[0]; $user2=$row[1]; $user3=$row[2]; $user4=$row[3]; $user5=$row[4]; $user6=$row[5]; un tad pec id liek random skaitļus iekša.. id kolona ir viena un tur nekas nav mainijies un skaitli aile ielikas katram cits.. Bet ar mysql_fetch_row tu tak nolasi VIENU ierakstu, nevis veselus 6... Varbūt labāk izmanot mysql_fetch_assoc, būs skaidrāka padarīšana while($row = mysql_fetch_assoc($data){ $user_id = $row['id']; //... } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.