Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-26-2006, 10:02 AM   PM User | #1
arne2
Regular Coder

 
Join Date: Aug 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
arne2 is an unknown quantity at this point
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
arne2 is offline   Reply With Quote
Old 08-26-2006, 10:18 AM   PM User | #2
GJay
Senior Coder

 
Join Date: Sep 2005
Posts: 1,791
Thanks: 5
Thanked 36 Times in 35 Posts
GJay is on a distinguished road
the endtime and rank are the same piece of information, why do you need to store it twice?
GJay is offline   Reply With Quote
Old 08-26-2006, 10:19 AM   PM User | #3
arne2
Regular Coder

 
Join Date: Aug 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
arne2 is an unknown quantity at this point
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?
arne2 is offline   Reply With Quote
Old 08-26-2006, 11:16 AM   PM User | #4
GJay
Senior Coder

 
Join Date: Sep 2005
Posts: 1,791
Thanks: 5
Thanked 36 Times in 35 Posts
GJay is on a distinguished road
yep, sounds about right.
GJay is offline   Reply With Quote
Old 08-26-2006, 11:34 AM   PM User | #5
arne2
Regular Coder

 
Join Date: Aug 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
arne2 is an unknown quantity at this point
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?
arne2 is offline   Reply With Quote
Old 08-26-2006, 02:15 PM   PM User | #6
Mwnciau
Regular Coder

 
Join Date: May 2006
Location: Wales
Posts: 820
Thanks: 1
Thanked 82 Times in 79 Posts
Mwnciau is on a distinguished road
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 02:18 PM..
Mwnciau is offline   Reply With Quote
Old 08-26-2006, 02:24 PM   PM User | #7
Mwnciau
Regular Coder

 
Join Date: May 2006
Location: Wales
Posts: 820
Thanks: 1
Thanked 82 Times in 79 Posts
Mwnciau is on a distinguished road
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;

Mwnciau is offline   Reply With Quote
Old 08-27-2006, 11:19 AM   PM User | #8
arne2
Regular Coder

 
Join Date: Aug 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
arne2 is an unknown quantity at this point
thank you very much
arne2 is offline   Reply With Quote
Old 08-27-2006, 04:07 PM   PM User | #9
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
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.
marek_mar is offline   Reply With Quote
Old 08-27-2006, 04:19 PM   PM User | #10
Mwnciau
Regular Coder

 
Join Date: May 2006
Location: Wales
Posts: 820
Thanks: 1
Thanked 82 Times in 79 Posts
Mwnciau is on a distinguished road
ah yes... i see my flaw... but it will probably work for what he wants.
Mwnciau is offline   Reply With Quote
Old 08-27-2006, 05:57 PM   PM User | #11
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
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.
marek_mar is offline   Reply With Quote
Old 08-27-2006, 06:23 PM   PM User | #12
arne2
Regular Coder

 
Join Date: Aug 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
arne2 is an unknown quantity at this point
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 06:28 PM..
arne2 is offline   Reply With Quote
Old 08-27-2006, 07:44 PM   PM User | #13
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
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.
marek_mar is offline   Reply With Quote
Old 08-27-2006, 07:46 PM   PM User | #14
thunderhoster
New Coder

 
Join Date: Jul 2006
Location: Portugal
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
thunderhoster is an unknown quantity at this point
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
thunderhoster is offline   Reply With Quote
Old 08-29-2006, 11:07 AM   PM User | #15
arne2
Regular Coder

 
Join Date: Aug 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
arne2 is an unknown quantity at this point
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.
arne2 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:02 AM.


Advertisement
Log in to turn off these ads.