Forum Moderators: coopster
I've gone to with some faction in my php site. i would need to save some value from the list option(or select option) into mysql database
<select name="faction" size="1">
<option value="0" selected>0</option>
<option value="0.25">1/4</option>
<option value="0.5">1/2</option> </select>
so i save the floating point value into the mysql instead of faction( i not sure whether i'm able to save faction into mysql or not, if yes what would be the data types then? ) but now i have to retieve the value from mysql to display it. the question sound this, how am i gonna change the floating point that i savad in mysql previously to faction? please suggest if you have a better way. sorry about my english, i would explain more in detail, if you guys can't understand it.
You could use floating point. I tend to use decimal because it's more precise. With floating point, you may see that the values you retrieve are not always the same values you save.
<how am i gonna change the floating point that i savad in mysql previously to faction?>
Since you're using a select list to choose the value, that means you have a finite number of possible values. The easiest in this case might be an associative array, with the key being the decimal value you've saved, and the value being a textual representation of the fraction. So if the value that you saved is $x, then $Fraction[$x] equals the textual representation of the fraction.
Trying to calculate the fraction from the saved decimal may not be a good idea, because there is not a one-to-one correlation. 0.25 can be 1/4, 2/8, 4/16, etc. You could also just save the fraction in your MySQL database as text, but this would make it harder to use in a calculation if needed. Or, you could save both the decimal form and the text form, one to use in math and one to display.
// use $Fraction[$x] equals the textual representation of the fraction.
would you explain more with the above statement? correct me if i'm wrong i tried to $Fraction[$data] ($data was retieve from database) but it return nothing. by just using $data would return the value which i saved in database.
//save both the decimal form and the text form
got to said it would be a pretty bright idea. but i would put to as my last choice. because i was try to save as little thing to databasa as possible.
-1. try not to used up too much web space by where i expected the database to grow very big later(it will right?)
-2. i afraid it would exceed the bandwidth given by my web host if i retieve too much data from the sql. (correct me if i'm wrong )
Your select list has the following options: 0, 1/4, and 1/2. So at the beginning of your retrieval script, you have something like this:
$Fraction = array("0" => "0", ".25" => "1/4", ".5" => "1/2");
...and so on, for however many options you have in the drop-down list:
So:
$data = ".25";
echo $Fraction[$data];
...should print "1/4".
<1. try not to used up too much web space by where i expected the database to grow very big later(it will right?)>
That depends on how many records you're going to add to it, but you definately want to keep it as small as possible while still giving you all of the functionality that you need. If using a finite list of values, like in a drop-down select list, I would use the option above. If using an infinite number of possibilities (where the user manually types in a value), then you'd want to save both values, because you will not be able to easily or accurately retrieve the fraction that was originally keyed in.
<2. i afraid it would exceed the bandwidth given by my web host if i retieve too much data from the sql. (correct me if i'm wrong )>
If the MySQL server and the web server are one in the same, then probably not. If the MySQL server is elsewhere on your hosting company's network, then it depends on whether or not they charge you bandwidth that just passes out of their network from your site, or if they charge you for all bandwidth generated by your site in and/or out of that machine.
FLOAT(precision) [UNSIGNED] [ZEROFILL]
A floating-point number. precision can be <=24 for a single-precision floating-point number and between 25 and 53 for a double-precision floating-point number. These types are like the FLOAT and DOUBLE types described immediately below. FLOAT(X) has the same range as the corresponding FLOAT and DOUBLE types, but the display size and number of decimals are undefined. In MySQL Version 3.23, this is a true floating-point value. In earlier MySQL versions, FLOAT(precision) always has 2 decimals. Note that using FLOAT may give you some unexpected problems as all calculations in MySQL are done with double precision. See section A.5.6 Solving Problems with No Matching Rows. This syntax is provided for ODBC compatibility.FLOAT[(M,D)] [UNSIGNED] [ZEROFILL]
A small (single-precision) floating-point number. Allowable values are -3.402823466E+38 to -1.175494351E-38, 0, and 1.175494351E-38 to 3.402823466E+38. If UNSIGNED is specified, negative values are disallowed. The M is the display width and D is the number of decimals. FLOAT without arguments or FLOAT(X) where X <= 24 stands for a single-precision floating-point number.DECIMAL[(M[,D])] [UNSIGNED] [ZEROFILL]
An unpacked floating-point number. Behaves like a CHAR column: ``unpacked'' means the number is stored as a string, using one character for each digit of the value. The decimal point and, for negative numbers, the `-' sign, are not counted in M (but space for these is reserved). If D is 0, values will have no decimal point or fractional part. The maximum range of DECIMAL values is the same as for DOUBLE, but the actual range for a given DECIMAL column may be constrained by the choice of M and D. If UNSIGNED is specified, negative values are disallowed. If D is omitted, the default is 0. If M is omitted, the default is 10. Prior to MySQL Version 3.23, the M argument must include the space needed for the sign and the decimal point.
So instead of
$fraction = .25;
$result = $number * $fraction;
You do
$fraction = 17 // i.e. one seventeenth, a commonly used fraction ;-)
$result = $number/$fraction;
That way any rounding only occurs at the very end so rounding errors don't accumulate.
Wouldn't that be both simplest and most accurate?
try to save as little thing to databasa as possible.
Oh yeah, forgot about that. You can't do much better than a TINYINT in terms of space either.
Tom