Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    jnt
    jnt is offline
    New Coder
    Join Date
    Mar 2011
    Location
    Underworld
    Posts
    15
    Thanks
    6
    Thanked 0 Times in 0 Posts

    PHP converting units code (advise)

    Hi, i am new in php and i want to make php page for converting units (lenght, time, weight etc.).

    But before i start, i wanted to ask: is it smart to do this in php (js or something else)? And how did i started, what do you think about this code below?

    Here is my idea:

    PHP Code:


    <?php

    $vre 
    $_POST['vrednost'];
    $jed $_POST['jed'];
    $ujed $_POST['ujed'];

    if (
    $jed == "celsius")

    switch (
    $ujed){
        case 
    "celsius":
            echo 
    "Same thing.";
            break;
        case 
    "farenheit":
            echo (
    number_format(($vre*(9/5)+32),2));
            break;    
        case 
    "kelvin":
            echo (
    number_format(($vre 273.15),2));
            break;        
    }

    elseif (
    $jed == "farenheit")

    switch (
    $ujed){
        case 
    "celsius":
            echo (
    number_format((($vre-32)*(5/9)),3));
            break;
        case 
    "farenheit":
            echo 
    "Same thing.";
            break;        
        case 
    "kelvin":
            echo (
    number_format((($vre+459.67)*(5/9)),3));
            break;    
    }

    elseif (
    $jed == "kelvin")

    switch (
    $ujed){
        case 
    "celsius":
            echo (
    number_format(($vre-273.15),2));
            break;
        case 
    "farenheit":
            echo (
    number_format(($vre * (9/5) - 459.67),2));
            break;        
        case 
    "kelvin":
            echo 
    "Same thing.";
            break;    
    }

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    I would use javascripting only because the conversion would be immediate,
    without a page refresh. You could also use AJAX, combination of PHP and
    javascripting. A good way to learn some programming.

    I would probably pick a better topic than the conversion thing. But perhaps
    this is a homework assignment (for a class)?


    .

  • Users who have thanked mlseim for this post:

    jnt (05-23-2011)

  • #3
    jnt
    jnt is offline
    New Coder
    Join Date
    Mar 2011
    Location
    Underworld
    Posts
    15
    Thanks
    6
    Thanked 0 Times in 0 Posts
    No, it's not homework , i finished school long time ago I just wanted to practice PHP through this.Well, maybe i choose bad example.Do you have some advice what to make?

    Btw, i had same idea, to use AJAX, but i'm new in js too

    And one more question: is this code i wrote too complicated (can it be more simpler)?

    p.s. sry for bad english


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •