...

View Full Version : Need help on simplifying script.. Again!



Taylor_1978
01-27-2004, 05:03 AM
Hiya,

I have a bad habit of writing scripts that are way too big and doing things the hard way - last time I posted like this a 30 line script turned into a 4 line script! LOL

So here is my script:


// Set all 4 players as the 4th seed.
$seed1 = 4;
$seed2 = 4;
$seed3 = 4;
$seed4 = 4;

// Move each player up one seed when their rating is higher.
if ($p1rating > $p2rating) { $seed1 = $seed1 - 1; } else { $seed2 = $seed2 - 1; }
if ($p1rating > $p3rating) { $seed1 = $seed1 - 1; } else { $seed3 = $seed3 - 1; }
if ($p1rating > $p4rating) { $seed1 = $seed1 - 1; } else { $seed4 = $seed4 - 1; }
if ($p2rating > $p3rating) { $seed2 = $seed2 - 1; } else { $seed3 = $seed3 - 1; }
if ($p2rating > $p4rating) { $seed2 = $seed2 - 1; } else { $seed4 = $seed4 - 1; }
if ($p3rating > $p4rating) { $seed3 = $seed3 - 1; } else { $seed4 = $seed4 - 1; }

// Insert seed numbers.
mysql_db_query($tourneys, "UPDATE info SET s1='$seed1', s2='$seed2', s3='$seed3', s4='$seed4' WHERE tid='$tid'");

// Place players into appropriate positions according to seed.
if ($seed1 == 1) { mysql_db_query($tourneys, "UPDATE info SET pos1='$p1' WHERE tid='$tid'"); }
if ($seed1 == 2) { mysql_db_query($tourneys, "UPDATE info SET pos3='$p1' WHERE tid='$tid'"); }
if ($seed1 == 3) { mysql_db_query($tourneys, "UPDATE info SET pos4='$p1' WHERE tid='$tid'"); }
if ($seed1 == 4) { mysql_db_query($tourneys, "UPDATE info SET pos2='$p1' WHERE tid='$tid'"); }
if ($seed2 == 1) { mysql_db_query($tourneys, "UPDATE info SET pos1='$p2' WHERE tid='$tid'"); }
if ($seed2 == 2) { mysql_db_query($tourneys, "UPDATE info SET pos3='$p2' WHERE tid='$tid'"); }
if ($seed2 == 3) { mysql_db_query($tourneys, "UPDATE info SET pos4='$p2' WHERE tid='$tid'"); }
if ($seed2 == 4) { mysql_db_query($tourneys, "UPDATE info SET pos2='$p2' WHERE tid='$tid'"); }
if ($seed3 == 1) { mysql_db_query($tourneys, "UPDATE info SET pos1='$p3' WHERE tid='$tid'"); }
if ($seed3 == 2) { mysql_db_query($tourneys, "UPDATE info SET pos3='$p3' WHERE tid='$tid'"); }
if ($seed3 == 3) { mysql_db_query($tourneys, "UPDATE info SET pos4='$p3' WHERE tid='$tid'"); }
if ($seed3 == 4) { mysql_db_query($tourneys, "UPDATE info SET pos2='$p3' WHERE tid='$tid'"); }
if ($seed4 == 1) { mysql_db_query($tourneys, "UPDATE info SET pos1='$p4' WHERE tid='$tid'"); }
if ($seed4 == 2) { mysql_db_query($tourneys, "UPDATE info SET pos3='$p4' WHERE tid='$tid'"); }
if ($seed4 == 3) { mysql_db_query($tourneys, "UPDATE info SET pos4='$p4' WHERE tid='$tid'"); }
if ($seed4 == 4) { mysql_db_query($tourneys, "UPDATE info SET pos2='$p4' WHERE tid='$tid'"); }


Ok, just to explain - this is based on part of my tournament script. All tournaments are in the one table. 'p1', 'p2' etc stands for "player 1's userid #' etc. Hence 'p1rating' is player 1's rating.

Once it works out who is seeded in what position, it places them in pos1, pos2, pos3, or pos4 which allows the "standings to know who goes in what part of the tree.

Make sense? Probably not! LOL

This is a small version of my script - we go up to 8 players - so it doubles in size - I am concerned though when I go up to 16, 32 and OMG 64!! lol

Any idea's?

Thanks in advance! :thumbsup:

fimi
01-27-2004, 05:44 AM
you do it yourself because if we all do it for you then you will never learn. That's very simple! use LOOPS.

last part looks something like this:

for ($i = 1; $i <= 4; $i++) {
for ($m = 1; $m <= 4; $m++) {
if ($seed$i == $m ) { mysql_db_query($tourneys, "UPDATE info SET pos1='$p$i' WHERE tid='$tid'"); }
if ($seed$i == $m+1) { mysql_db_query($tourneys, "UPDATE info SET pos3='$p$i' WHERE tid='$tid'"); }
if ($seed$i == $m+2) { mysql_db_query($tourneys, "UPDATE info SET pos4='$p$i' WHERE tid='$tid'"); }
if ($seed$i == $m+3) { mysql_db_query($tourneys, "UPDATE info SET pos2='$p$i' WHERE tid='$tid'"); }
}
}

Taylor_1978
01-27-2004, 08:48 AM
Firstly...

Thank you for your help/suggestion - much appreciated.

Secondly...

I was not asking people to do it for me. If I wanted people to do it for me, I would have not written any script. I would have just said what I wanted and asked someone to do it for me.

AND... If we all just "did it ourselves because we will never learn", then what would be the point of this forum?

I for one get frustrated when poeple come to these forums and do not bother to read up on how to use a specific function - HOWEVER, what is the harm in asking what function would be best suited?

Again, thanks for your help - but try not to jump so quick on people just because they may not be as experienced as you!

Taylor.

taydag
01-27-2004, 10:41 AM
My friend
i am a newbie programmer as well
Only think i suggrst you is "start learning it from LOOPS ands FUNCTIONS "
belive me it will take max. 30 minutess after reading a well organized tutoria, to write your first loop & function including code.
:)
just a suggestion from another newbie

Spookster
01-27-2004, 04:44 PM
In programming whenever you are doing something repeatedly or using the the same code or pretty much the same code over and over then that screams out use a function or a loop. In this case I think a function would be best suited as far as handling most of this. Just set up a function and pass the parameters in that you need for example:




function updatePlayerPosition($seed, $postion, $tid){
//code for upadting player position
}



Also having to do that many conditional checks seems a bit much. You should look to see if that can be simplified or eliminated. If all of those conditions are etched in stone meaning if each of those seeds are are always going to be associated with a specific position then you could create a multidimensional array to associate those values together and then you would not need to do all of those conditional checks and assignments. You would just need to specify which element in the array to use.

fimi
01-27-2004, 09:31 PM
Taylor_1978, sorry if i caused you anything but i didn't mean it in a bad way...i only did that part for you so that it can get you started.

Again,sorry and all im saying is that you have use more resources and use the documentation alot...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum