2011-11-25 65 views
-2

我想創建一個這樣的腳本很長時間。我在下面試過,由於某種原因沒有顯示。其他人是否有類似這樣的例子。你認爲什麼是一種有效的方式來顯示這一點。這是基於用戶輸入一個值。計算字節/ kb/mb/tb/gb的簡單方法

<?php 


    if($_GET['step'] == 2) { 

    $bytes = $_GET['bytes']; 

    echo $bytes; 

    $kilobyte = 1024; 
$megabyte = $kilobyte * 1024; 
$gigabyte = $megabyte * 1024; 
$terabyte = $gigabyte * 1024; 

if (($bytes >= 0) && ($bytes < $kilobyte)) { 
    return $bytes; 

} 
    if (($bytes >= $kilobyte) && ($bytes < $megabyte)) 
    { 
    return $kb = round($bytes/$kilobyte); 

} 
    if (($bytes >= $megabyte) && ($bytes < $gigabyte)) 
    { 
    return $mb = round($bytes/$megabyte); 
} 
    if (($bytes >= $gigabyte) && ($bytes < $terabyte)) 
    { 
    return $gb = round($bytes/$gigabyte); 

} 
    if ($bytes >= $terabyte) 
    { 
    return $tb = round($bytes/$terabyte); 
} 
    else { 
    return $bytes; 
} 

    $content .= "<h2>Details of Memory Size </h2> 

    <table> 

     <tr> 
          <th>Bytes</th> 
      <th>KB</th>     
      <th>MB</th> 
      <th>GB</th> 
          <th>TB</th 

     </tr>"; 





      $content .= "<tr > 
        <td> $bytes</td> 
              <td>$kb </td> 
              <td>$mb</td> 
              <td>$gb</td> 
              <td>$tb</td> 

             </tr>  
            "; 




    } 

    else { 


    $content = " <form action='index.php?step=2' method='post'> 
    <table> 
    <tr> 
    <th>Enter the number of kilobytes on unix drive</th> 
    <td><input type='text' name='bytes'/></td> 
    </tr> 
    <tr> 
    <td> 
    <input type='submit' /> 
    </td> 
    </table> 
    </form>"; 

    echo $bytes; 
    } 


?> 
+0

你爲什麼要在那裏使用?你有沒有向我們展示什麼? –

回答

-1

我發現前一陣子在PHP文檔評論轉換字節人類可讀的支持SI和二進制前綴的函數:

function size_readable($size, $max = null, $system = 'bi', $retstring = '%01.2f %s') 
{ 
    // Pick units 
    $systems['si']['prefix'] = array('B', 'K', 'MB', 'GB', 'TB', 'PB'); 
    $systems['si']['size'] = 1000; 
    $systems['bi']['prefix'] = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'); 
    $systems['bi']['size'] = 1024; 
    $sys = isset($systems[$system]) ? $systems[$system] : $systems['si']; 

    // Max unit to display 
    $depth = count($sys['prefix']) - 1; 
    if ($max && false !== $d = array_search($max, $sys['prefix'])) { 
     $depth = $d; 
    } 

    // Loop 
    $i = 0; 
    while ($size >= $sys['size'] && $i < $depth) { 
     $size /= $sys['size']; 
     $i++; 
    } 

    return sprintf($retstring, $size, $sys['prefix'][$i]); 
} 
0

是你試圖做這樣的事情?

<html> 

    <head> 
    <title>Byte scale calculator</title> 
    </head> 

    <body> 
<?php 

    if ($_GET['step'] == 2) { 

    // Handle submissions 

    $bytes = $_GET['bytes']; 

    // Calculate KB 
    $kilobyte = 1024; 
    $kb = round($bytes/$kilobyte, 2); 

    // Calculate MB 
    $megabyte = $kilobyte * 1024; 
    $mb = round($bytes/$megabyte, 2); 

    // Calculate GB 
    $gigabyte = $megabyte * 1024; 
    $gb = round($bytes/$gigabyte, 2); 

    // Calculate TB 
    $terabyte = $gigabyte * 1024; 
    $tb = round($bytes/$terabyte, 2); 

?> 
    <h2>Details of Memory Size </h2> 
    <table> 
     <tr> 
     <th>Bytes</th> 
     <th>KB</th>     
     <th>MB</th> 
     <th>GB</th> 
     <th>TB</th> 
     </tr> 
     <tr> 
     <td><?php echo $bytes; ?></td> 
     <td><?php echo $kb; ?></td> 
     <td><?php echo $mb; ?></td> 
     <td><?php echo $gb; ?></td> 
     <td><?php echo $tb; ?></td> 
     </tr> 
    </table>  
<?php } ?> 

    <form action='' method='get'> 
     <input type='hidden' name='step' value='2' /> 
     <!-- You had the form method set to post, but you were trying to fecth data from $_GET --> 
     <table> 
     <tr> 
      <th>Enter the number of kilobytes on unix drive</th> 
      <td><input type='text' name='bytes' /></td> 
     </tr> 
     <tr> 
      <td><input type='submit' /></td> 
     </tr> 
     </table> 
    </form> 
    </body>   

</html> 

有太多的變化要注意他們,所以如果有什麼不明白隨時問在底部的註釋。