Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    528
    Thanks
    8
    Thanked 8 Times in 8 Posts

    Need help on simplifying script.. Again!

    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:
    PHP Code:
    // 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!

  • #2
    New Coder
    Join Date
    Dec 2003
    Posts
    80
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    PHP Code:
    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'"); }
        }


  • #3
    Regular Coder
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    528
    Thanks
    8
    Thanked 8 Times in 8 Posts
    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.

  • #4
    New Coder
    Join Date
    Jan 2004
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #5
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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:

    PHP Code:

    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.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #6
    New Coder
    Join Date
    Dec 2003
    Posts
    80
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •