Solved

fractions

Posted on 2013-05-23
4
203 Views
Last Modified: 2013-05-24
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
0
Comment
Question by:doctorbill
4 Comments
 
LVL 12

Assisted Solution

by:adrian_brooks
adrian_brooks earned 167 total points
ID: 39191601
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
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 166 total points
ID: 39191628
@AB is right, save them as text.  PHP does not understand fractions like that.
0
 
LVL 20

Accepted Solution

by:
Mark Brady earned 167 total points
ID: 39191659
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;

?>

Open in new window


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
 

Author Closing Comment

by:doctorbill
ID: 39193906
Food for thought
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now