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 07-02-2007, 04:32 PM   PM User | #1
I3arry
New Coder

 
Join Date: Dec 2004
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
I3arry is an unknown quantity at this point
ratio of row1 and row 2

I have a row called totalkills and one called totaldeaths, how could i have it return the ratio of them both EG totalkills = 100 totaldeaths = 10 ratio = 10/1

(not sure if php or sql forum is best!)


Thanks for your time
I3arry is offline   Reply With Quote
Old 07-03-2007, 01:48 PM   PM User | #2
ralph l mayo
Regular Coder

 
ralph l mayo's Avatar
 
Join Date: Nov 2005
Posts: 951
Thanks: 1
Thanked 31 Times in 29 Posts
ralph l mayo is on a distinguished road
Is this a trick question or something? SELECT totalkills/totaldeaths FROM table
ralph l mayo is offline   Reply With Quote
Old 07-03-2007, 03:37 PM   PM User | #3
I3arry
New Coder

 
Join Date: Dec 2004
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
I3arry is an unknown quantity at this point
...

and how does that helpget the ratio?
I3arry is offline   Reply With Quote
Old 07-03-2007, 03:59 PM   PM User | #4
whizard
Senior Coder

 
whizard's Avatar
 
Join Date: Jan 2005
Location: Philadelphia, PA, USA
Posts: 1,457
Thanks: 10
Thanked 37 Times in 37 Posts
whizard will become famous soon enoughwhizard will become famous soon enough
That is the query you use, and it will return the ratio you are looking for.

Dan
__________________
If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.
Step 1: Learn. Step 2: Search. Step 3: Post here.
whizard is offline   Reply With Quote
Old 07-04-2007, 02:34 AM   PM User | #5
ralph l mayo
Regular Coder

 
ralph l mayo's Avatar
 
Join Date: Nov 2005
Posts: 951
Thanks: 1
Thanked 31 Times in 29 Posts
ralph l mayo is on a distinguished road
If you're aiming to get fractions from the table you can use this:

Code:
DELIMITER ~~

CREATE FUNCTION normalized_fraction(a int, b int) RETURNS text
LANGUAGE SQL
DETERMINISTIC
NO SQL
BEGIN
	DECLARE tmp, orig_a, orig_b int;

	IF b = 0 THEN
		RETURN NULL;
	END IF;

	SET orig_a = a;
	SET orig_b = b;

	REPEAT
		SET tmp = b;
		SET b = a % b;
		SET a = tmp;
	UNTIL b = 0
	END REPEAT;

	SET orig_a = orig_a / a;
	SET orig_b = orig_b / a;
	RETURN CONCAT(orig_a, '/', orig_b);
END~~
After which:

Code:
mysql> SELECT *, totalkills/totaldeaths AS ratio, normalized_fraction(totalkills, totaldeaths) AS fractional_ratio FROM test;
+------------+-------------+----------+------------------+
| totalkills | totaldeaths | ratio    | fractional_ratio |
+------------+-------------+----------+------------------+
|       1000 |          10 | 100.0000 | 100/1            |
|         10 |        1000 |   0.0100 | 1/100            |
|         10 |           0 |     NULL | NULL             |
|          0 |          10 |   0.0000 | 0/1              |
+------------+-------------+----------+------------------+
4 rows in set (0.00 sec)

Last edited by ralph l mayo; 07-04-2007 at 02:43 AM.. Reason: Catch to make b == 0 a little less catastrophic
ralph l mayo is offline   Reply With Quote
Old 07-04-2007, 05:34 PM   PM User | #6
I3arry
New Coder

 
Join Date: Dec 2004
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
I3arry is an unknown quantity at this point
ok, (totalkills/totaldeaths) as totalkd works

How about doing the math when entering the data

I tried

PHP Code:
$totalkd trim($playersArray[$i][2])/trim($playersArray[$i][3]); 
and

PHP Code:
$totalkd $totalkills/$totaldeaths
And they both return single numbers

edit fixed, forgot to set it to float

Last edited by I3arry; 07-04-2007 at 07:17 PM..
I3arry 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 01:54 AM.


Advertisement
Log in to turn off these ads.