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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    Regular Coder
    Join Date
    Aug 2006
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts

    saving mysql order number

    HI,
    i have created a script that randomly creates endtimes (of a race).
    So like this :
    PHP Code:
    $endtime1=mt_rand(1,5); 
    and so on for 5times (so till $endtime5).
    Endtime1 is the endtime of horse nr 1
    Endtime2....................................2,...

    The endtimes are written in the database in the table 'horsescores'.
    (id(auto_increment),horsenr,endtime,rank).
    In horsenr the number of the horse (1-5)is written. In the endtime column the endtimes are written.
    I now want to write the rank (depending on the endtimes, so the horse with the smallest endtime is the winner, etcetc untill the loser (5th).
    How can i do this?
    I thought about selecting all horses and order them by endtime with an sql query but i don't exactly now how i can save that ordering in ranknumbers.
    And maybe there is a better way?

    Thank you for you help

  • #2
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    the endtime and rank are the same piece of information, why do you need to store it twice?

  • #3
    Regular Coder
    Join Date
    Aug 2006
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    because i want to make the game look real by adding an endtime into the scorebord.
    But you're probably right and think i just realized what you mean.
    Can i (on the scorebord), order them by endtime ASC and adding the corresponding rank in the 'rank' column?
    How?
    With an $i ?
    And when it starts $i = 1 (so first), and use a while loop with $i++ everytime?

  • #4
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    yep, sounds about right.

  • #5
    Regular Coder
    Join Date
    Aug 2006
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    cool ! I'll try it now !
    About the mt_rand
    is there a way to use decimal numbers, for example an mt_rand between 9,11 and 12,23?

  • #6
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    I dont think so... you could do :

    PHP Code:
    $whole mt_rand(1020);
    $dec mt_rand(099);

    $number $whole '.' $dec
    Last edited by Mwnciau; 08-26-2006 at 03:18 PM.

  • #7
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    extending that:
    PHP Code:
    function my_rand_dec($min$max){
    list (
    $minwhole$mindec) = explode('.'$min2);
    list (
    $maxwhole$maxdec) = explode('.'$max2);
    $whole mt_rand($minwhole$maxwhole);
    $dec mt_rand($mindec$maxdec);
    return 
    $whole '.' $dec;


  • #8
    Regular Coder
    Join Date
    Aug 2006
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you very much

  • #9
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by Mwnciau
    extending that:
    PHP Code:
    function my_rand_dec($min$max){
    list (
    $minwhole$mindec) = explode('.'$min2);
    list (
    $maxwhole$maxdec) = explode('.'$max2);
    $whole mt_rand($minwhole$maxwhole);
    $dec mt_rand($mindec$maxdec);
    return 
    $whole '.' $dec;

    The decimal restrictions should only apply when the random number is either $minwhole or $maxwhole. When called with for ex. 20.8 and 30.9 you won't get a 25.1
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #10
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    ah yes... i see my flaw... but it will probably work for what he wants.

  • #11
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    How about like this:
    PHP Code:
    <?php
    function float_mt_rand($min$max)
    {
        
    $i 0;
        
    $j 0;
        while((int) 
    $min != (float) $min)
        {
            
    $min *= 10;
            
    $i++;
        }
        while((int) 
    $max != (float) $max)
        {
            
    $max *= 10;
            
    $j++;
        }
        
    $i max($i$j);

        return 
    mt_rand($min$max) / pow(10$i);
    }
    var_dump(float_mt_rand(1.24493.1));
    ?>
    The only flaw is that the numbers can't get too long unless yoy will use BCMath or GMP.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #12
    Regular Coder
    Join Date
    Aug 2006
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Mwnciau & marek_mar
    thank you both for your efforts in helping me
    About your code, Mwnciau, when i tried it it only returned decimal numbers of .0 and .1 so( 174.1,154.1,4564.1:456465.1,......)
    so that's not really what i wanted.
    About your code marek_mar, seems to work perfectly random, but it prints float() and int() with it, can you delete that? Thank you (because i just want to store the value in a variable like for example $randnr=float_mt_rand(min,max); for that it should only be a number right? (sorry i'm a total noob )

    Also about your code marek_mar, when i tried it (with your example numbers of min and max), i almost all the time gotnumbers 0,985 and 1,6 and never like 13.2. Why? (i don't understand your function ).
    Also : i should have numbers varying between 9.12 and 14.1, should i just enter that numbers in the min and max?

    THANK YOU BOTH AGAIN!
    Last edited by arne2; 08-27-2006 at 07:28 PM.

  • #13
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by arne2
    About your code marek_mar, seems to work perfectly random, but it prints float() and int() with it, can you delete that? Thank you (because i just want to store the value in a variable like for example $randnr=float_mt_rand(min,max); for that it should only be a number right? (sorry i'm a total noob )
    Yes, the int/float is from var_dump(). It is useful to know which type a variable has sometimes and that's why I use it.
    Quote Originally Posted by arne2
    Also about your code marek_mar, when i tried it (with your example numbers of min and max), i almost all the time gotnumbers 0,985 and 1,6 and never like 13.2. Why? (i don't understand your function ).
    I'm sorry this is a bug. This is version is corrected.
    PHP Code:
    <?php
    function float_mt_rand($min$max)
    {
        
    $i 0;
        while((int) 
    $min != (float) $min || (int) $max != (float) $max)
        {
            
    $min *= 10;
            
    $max *= 10;
            
    $i++;
        }
        return 
    mt_rand($min$max) / pow(10$i);
    }
    var_dump(float_mt_rand(1.24493.1));
    ?>
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #14
    New Coder
    Join Date
    Jul 2006
    Location
    Portugal
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is not the best way to solve your problem (it can occasionally cause some delay) but the quickest:

    Code:
    <?php
    
    function my_rand_dec($min, $max){
        $whole = 0;
        $dec = 0;
        
        $maxwhole = explode('.', $max, 2);
        $maxwhole = $maxwhole[0];
        
        $minwhole = explode('.', $min, 2);
        $minwhole = $minwhole[0];
    
        while (($whole . '.' . $dec) == 0 || ($whole . '.' . $dec) > $max || ($whole . '.' . $dec) < $min) {
            $whole = mt_rand($minwhole, $maxwhole);
            $dec = mt_rand(0, 99);
        }
        
        return $whole . '.' . $dec;
    }
    
    ?>
    Liked my post? want my help in a project? MSN: geral'at'brunobernardino.com

  • #15
    Regular Coder
    Join Date
    Aug 2006
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you both i'm going to test it right away !
    Could anyone of you comment your code because i tried but i can't really figure out what everything means.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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