Pages

Selasa, 04 September 2012

PHP (Konversi Bilangan Menggunakan Radio Button)

Okeh teman - teman :D, ini postingan selanjutnya tentang PHP, yaitu coding untuk membuat program pengkonversian bilangan menggunakan radio button...

Sumonggo dicoba nggih, selamat mencoba :D
____________________________________________________________________________

<?php
if(isset($_POST['submit'])){
$nama=$_REQUEST['name'];
    if(isset($_POST['gender'])){
        $gender=$_REQUEST['gender'];
    }
    else{
        $gender='';
    }
$cek='';
}

else{
    $nama='';
    $gender='';
    $cek='Tolong Diisi Lengkap Ya!!!';
}
?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Login Untuk Konversi</title>
            
    </head>
    <body>
      <h1 class='a'>
<?php echo $cek ?></h1>
<br>
        <form action="Konversi.php" method="post" name="from">
            <p>
<h3>
Name : <input placeholder="Nama"
required="required" type="text" name="nama" size ="30" value="<?php
echo $nama ?>"/></h3>
</p>
<p>
<h3>
Gender :
                <input type="radio" name="gender" value="L"
                               <?php ($gender=="L")? print
'checked=""' : print ''; ?>/>
                Laki
                <input type="radio" name="gender" value="P"
                       <?php ($gender=="P")? print 'checked=""' :
print ''; ?>/>
                Perempuan
            </h3>
</p>
<center><input type="submit" name="submit" value="Enter"></center>
        </form>
    </body>
</html>
____________________________________________________________________________

Eits eits eits, nih codingan di atas belum selesai, jadi coding di atas memanggil file "Konversi.PHP" teman - teman... Dan file "Konversi.PHP" codingannya ada di bawah ini, selamat mencoba ya...
____________________________________________________________________________

<?php
error_reporting(0);
$des = $_REQUEST['des'];//deklarasi var desimal
$option = $_REQUEST['option'];//deklarasi var pilihan
//Membuat function bin untuk konversi ke biner
    function bin($bil){
      $decimal= $bil;
      $ori=$decimal;;
      while ($decimal>0){ //Looping memutuskan apakah inputan 1 atau 0 yang ditampilkan
                    if($decimal%2 == 0){ // menampbah 0
                        $binary .= 0; //$binary=0+$binary
                        $decimal /= 2; //$decimal=$decimal/2
                    }
                    else{   //menambah 1
                        $binary .= 1; //$binary=1+$binary
                        $decimal = ($decimal/2)-0.5;

                    }
                }
                $result = strrev($binary);
      return "Biner dari : $ori adalah : $result <br/>"; //fungsi ini mereturn hasil
    }
//Membuat function oct untuk konversi ke octal

    function oct($bil){
        $des= $bil;
        $ori=$des;
        $oct='';
        while($des>0){
            $hasil=$des%8;
                switch($hasil){
                    case 0: $oct.="0"; break;
                    case 1: $oct.="1"; break;
                    case 2: $oct.="2"; break;
                    case 3: $oct.="3"; break;
                    case 4: $oct.="4"; break;
                    case 5: $oct.="5"; break;
                    case 6: $oct.="6"; break;
                    case 7: $oct.="7"; break;
                    default : break;
                }
                if($des/8==0){
                    $sisa=($des%8);
                    $des=$sisa;
                }
                else{
                    $sisa=($des/8);
                    $des=$sisa%8;
                }
        }
        $result = strrev($oct);
         return "Octal dari : $ori adalah : $result <br/>"; //funngsi ini mereturn hasil
    }
//Membuat function hex untuk konversi ke hexa
    function hex($bil){
        $des= $bil;
        $ori=$des;
        $hex='';
        while($des>0){
        $hasil=$des%16;
            switch($hasil){
                case 0: $hex.="0"; break;
                case 1: $hex.="1"; break;
                case 2: $hex.="2"; break;
                case 3: $hex.="3"; break;
                case 4: $hex.="4"; break;
                case 5: $hex.="5"; break;
                case 6: $hex.="6"; break;
                case 7: $hex.="7"; break;
                case 8: $hex.="8"; break;
                case 9: $hex.="9"; break;
                case 10: $hex.="A"; break;
                case 11: $hex.="B"; break;
                case 12: $hex.="C"; break;
                case 13: $hex.="D"; break;
                case 14: $hex.="E"; break;
                case 15: $hex.="F";
                default : break;
            }
            if($des/16==0){
                $sisa=($des%16);
                $des=$sisa;
            }
            else{
                $sisa=($des/16);
                $des=$sisa%16;
            }
            }
         $result = strrev($hex);
         return "Hexa dari : $ori adalah : $result <br/>"; //funngsi ini mereturn hasil
    }
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Converter Option for Biner by Firayz</title>
    </head>
    <body>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>"
                method="post" name="form1">
            <p>Inputkan bilangan desimal :
                <input type="text" name="des" value="<?php echo $des ?>" /></p>
            <p>Pilih konversi : <br/>
                <input type="radio" name="option" value="bin"
                    <?php ($option=="bin") ? print 'checked=""': print ''; ?>/>
                        Desimal to Biner<br/>
                <input type="radio" name="option" value="hex"
                    <?php ($option=="hex") ? print 'checked=""': print ''; ?>/>
                        Desimal to Hexa<br/>
                <input type="radio" name="option" value="oct"
                    <?php ($option=="oct") ? print 'checked=""': print ''; ?>/>
                        Desimal to Octal<br/>
            </p>
            <input type="submit" name="submit" value="Submit"/>
        </form>
        <?php
            if(isset($_POST['des'])){ //apakah data tersubmit?
                /*mambuat variable untuk menyimpan data yang dikirim*/
                $des = $_REQUEST['des'];
                $option = $_REQUEST['option'];
                //cek apakah data yang dikirim tidak kosong ?
                if($des=='' || $option==''){
                    echo '<h2>Maaf data Kurang Lengkap</h2>';
                }
                else{
                    //cek jenis kelamin
                    switch($option){
                        case 'bin' : echo "<h3>".bin($des)."</h3>"; break;
                        case 'hex' : echo "<h3>".hex($des)."</h3>"; break;
                        case 'oct' : echo "<h3>".oct($des)."</h3>"; break;
                        default : break;
                    }
                }
                echo "<Konversi.php>Reset</a>"; //hasil
            }
         ?>
    </body>
</html>
____________________________________________________________________________

Kalo aku udah berhasil membuat nih program, nih di bawah buktinya :P

 



0 komentar:

Posting Komentar

 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Learn from yesterday ,

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

live for today ,

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

and hope for tomorrow...