# fractions

Is it possible for me to add values such as 1 1/2, 3/3/4, 5 1/2, 100 1/2,   etc into a database with a php form and then return the values as fractions to a php page

The values are picture measurements
###### Who is Participating?

x

Principal Data EngineerCommented:
Yes store the textural values ina varchar column as suggested. When you get the values back you do this.

``````<?php

// a sample value
\$val = '2 5/8';

// explode into separate pieces (whole numbers and fractions)
\$val = array_map('trim', explode(' ', \$val));

// explode the two parts of the fraction
\$fraction = array_map('trim', explode('/', \$val[1]));

// now perform the mathmatical process
\$decimal = \$val[0] + (\$fraction[0] / \$fraction[1]);

echo \$decimal;

?>
``````

of course my suggestion is going to return the decimal not the fraction. Working it backwards to turn it into a fraction can be done but would be a bit of code.
0

Senior Web DeveloperCommented:
The only way to effectively perform what you're asking for, would be to take the textual representation of the fraction and execute the mathematical division of the fraction part of the text and then add the whole number to the result. After which, you would store that decimal value into your database.

When you retrieve the stored value, you will likely have a much harder time determining what the original textual fraction was by that value. So, you might want to store the textual representation in a varchar column in that same table so that you can display that versus a decimal value.

Other than that, I honestly can't envision any other way to accomplish this task.

Hope this helps.

~AB
0

Fixer of ProblemsCommented:
@AB is right, save them as text.  PHP does not understand fractions like that.
0

TechAuthor Commented:
Food for thought
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.