Jump to content
php.lv forumi

Ka sadalit atrashanas vietu.


shurix

Recommended Posts

Ir 4 cilveki tabula. Katram vajadzetu pievienot unikaalu skaitli (vietu) no 1 - 4. Un taa lai tas butu random nevis katram cilvekam pec kartas.

Apmeram ta ka te lai vieta butu unikaala un nejausha.

id | vards | vieta

1 andris 3

2 rudis 1

3 maiga 4

4 anda 2

Link to comment
Share on other sites

viens no risinājumiem:

 

mysql_query('UPDATE tabula SET vieta=0');
for($x=1;$x<=4;$x++){
mysql_query(
"UPDATE tabula AS t1 SET 
vieta = $x 
WHERE t1.id IN (
   SELECT id FROM tabula AS t2
   WHERE t2.vieta = 0
   ORDER BY RAND() LIMIT 1
)"
);
}

Link to comment
Share on other sites

viens no risinājumiem:

 

mysql_query('UPDATE tabula SET vieta=0');
for($x=1;$x<=4;$x++){
mysql_query(
"UPDATE tabula AS t1 SET 
vieta = $x 
WHERE t1.id IN (
   SELECT id FROM tabula AS t2
   WHERE t2.vieta = 0
   ORDER BY RAND() LIMIT 1
)"
);
}

 

Kaut kas man tur tomer nedarbojas. Varbut ir vel kaadi citi varianti?

Link to comment
Share on other sites

  • 2 weeks later...

pamēģini šitā:

 

if(!(mysql_query('UPDATE tabula SET vieta=0'))) die (mysql_error());
for($x=1;$x<=4;$x++){
	if(!(mysql_query(
					"UPDATE tabula AS t1 SET 
					vieta = $x 
					WHERE t1.id IN (
						   SELECT id FROM tabula AS t2
						   WHERE t2.vieta = 0
						   ORDER BY RAND() LIMIT 1)"
	))) die (mysql_error());

}

 

 

Man jau škiet, ka tava tabula nesaucas `tabula`

Link to comment
Share on other sites

Cik saprotu vainiigs serveris? Varbut ir citi varianti prieksh Client API version 5.0.45

Client API ir klienta api. Tas nenorāda ne nieka par to, ko uztur vai neuztur serveris. Servera versiju vajag skatītites.

Link to comment
Share on other sites

Beigas uztaisiju shaadi. NEzinu cik tas pareizi bet vismaz darbojas :D

for($x=1;$x<=2;$x++){
	$vieta_query = mysql_query("SELECT id FROM tabula AS t2 WHERE t2.vieta = 0 ORDER BY RAND() LIMIT 1");
	$vieta_row = mysql_fetch_row($vieta_query);
	if(!(mysql_query("UPDATE tabula AS t1 SET vieta = '$x' WHERE t1.id = '$vieta_row[0]'"
	))) die (mysql_error());

Link to comment
Share on other sites

×
×
  • Create New...