0
\$\begingroup\$

I am creating a volume converter. It allows users to enter a value and select the unit to convert from and the one it will be converted into. It uses a <select> tag for user input selection.

The <select> looks like this:

<select name="from">
 <option value=0>--select--</option>
 <option value=1>cm3</option>
 <option value=2>m3</option>
 <option value=3>Feet3</option>
 <option value=4>Gallons(UK)</option>
 <option value=5>Gallons(US)</option>
 <option value=6>Inches3</option>
 <option value=7>Litres</option>
 <option value=8>Yards3</option>
 <option value=9>Quart(UK)</option>
</select>

And there is another one similar to this with the name "to".

After that I did the following in PHP:

$from=$_POST['from'];
$to=$_POST['to'];
function assign($from,$val)
 {
 switch ($from)
 {
 case 1:
 $fromu="cm<sup>3</sup>";
 $cm=1;
 $me=0.000001;
 $ft=0.000035315;
 $gk=0.000219969;
 $gs=0.000264172;
 $in=0.061024;
 $li=0.001;
 $ya=0.000001308;
 $qtk=0.000879877;
 break;
 case 2:
 $fromu="m<sup>3</sup>";
 $cm=1000000;
 $me=1;
 $ft=35.32;
 $gk=220;
 $gs=264;
 $in=61024;
 $li=1000;
 $ya=1.308;
 $qtk=879.877;
 break;

There are 7 more cases based on the html select "from" and each holds a value of conversion for each unit relative to the selection made.

It ends with:

 echo "<br><br><table align=center>
 <tr><td><h3> ",$val," ",$fromu," equivalent is </h3></td></tr>
 <tr align=right><td><u> ",(double)($val*$cm),"</u> CentiMeter<sup>3</sup></td></tr>
 <tr align=right><td><u> ",(double)($val*$me),"</u> Meter<sup>3</sup></td></tr>
 <tr align=right><td><u> ",(double)($val*$ft),"</u> Feet<sup>3</sup></td></tr>
 <tr align=right><td><u> ",(double)($val*$gk),"</u> Gallons(UK)</td></tr>
 <tr align=right><td><u> ",(double)($val*$gs),"</u> Gallons(US)</td></tr>
 <tr align=right><td><u> ",(double)($val*$in),"</u> Inches<sup>3</sup></td></tr>
 <tr align=right><td><u> ",(double)($val*$li),"</u> Litres</td></tr>
 <tr align=right><td><u> ",(double)($val*$ya),"</u> Yards<sup>3</sup></td></tr>
 <tr align=right><td><u> ",(double)($val*$qtk),"</u> Quart(UK)</td></tr></table>";
 }
 assign($from,$val);
}

How do I optimize this in a way that I can save all the conversion values (I'm thinking possibly in an associative array) and still be able to display one conversion based on user selection in the "to" select instead of outputting all of the converted units as seen above?

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked May 5, 2016 at 13:07
\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

Convert the from to a single unit, and convert that single unit to to. You don't have to compute conversions for every single pair.

answered May 5, 2016 at 14:50
\$\endgroup\$
1
  • \$\begingroup\$ Can you please clarify on how that can be done? Thanks \$\endgroup\$ Commented May 5, 2016 at 17:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.