如何以MB为单位显示总大小


How to display the total size in MB

不知道有没有人能帮我快速解决这个问题。

我有一个php脚本,它工作得很好,但是,我希望它以MB而不是kb显示Total Memory

<div class="TitleBar">VM List</div>   
<?php
    $conn = pg_connect("host=".MGRCONFIG_DB_HOST."  port=".MGRCONFIG_DB_PORT." dbname=".MGRCONFIG_DB_NAME. " user=" .MGRCONFIG_DB_USER." password=".MGRCONFIG_DB_PASSWORD);
    if (!$conn) 
    {
        AddLog("vmlist.php","Could not connect to Database",ERR_DEBUG_HIGH);        
    }
    $query = "select guid,vmname,hostid,guestosname,cputype,cpucycle,totalmemory from vmobjects order by vmname";
    $result = pg_query($conn,$query);
    AddLog("vmlist.php","Query: ".$query,ERR_DEBUG_HIGH);
    $guid_array=Array();
    $lastname_array=Array();
    $firstname_array=Array();
    $osname_array=Array();
    $cptype_array=Array();
    $cpcycle_array=Array();
    $tom_array=Array();
    $no_of_record = 0;              
    while ($row = pg_fetch_array($result)) 
    {   
        $num_rows=0;
        $subquery = "select bussinessappid from bussinessapppolicyassoc where vmid ='".$row[0]."'";
        AddLog("vmlist.php","Query: ".$query,ERR_DEBUG_LOW);
        $subresult = pg_query($conn,$subquery);
        $num_rows=pg_num_rows($subresult);
        $guid_array[$no_of_record] = $row[0].'|'.$num_rows;
        $lastname_array[$no_of_record] = $row[1];
        $hostid = $row[2];
        $osname_array[$no_of_record] = $row[3];
        $cptype_array[$no_of_record] = $row[4];
        $cpcycle_array[$no_of_record] = $row[5];
        $tom_array[$no_of_record] = $row[6];                    
        $query = "select name from hosts where guid='".$hostid."'";
        $result1 = pg_query($conn,$query);
        AddLog("vmlist.php","Query: ".$query,ERR_DEBUG_HIGH);
        while ($row1 = pg_fetch_array($result1)) 
        {
             $firstname_array[$no_of_record] = $row1[0];
        }                       
        $no_of_record++;                    
    }
    pg_close($conn);
?>
      <table class="objList" cellspacing="0" cellpadding="0" id="vmtable">
        <tr>
          <td class="tbHeader"></td>
          <!--<td class="tbHeader"></td>
          <td class="tbHeader"></td>-->
          <td class="tbHeader">Name </td>
          <td class="tbHeader">Hostname</td>
          <td class="tbHeader">OS Name</td>
          <td class="tbHeader">CPU Type</td>
          <td class="tbHeader">CPU Cycle</td>
          <td class="tbHeader">Total Memory</td>


        </tr>
        <?php
            for ($i =0; $i < $no_of_record; $i++)
            {
                echo "<tr class='"objList'" id='"".$i."'">";
                echo "<td height='"20'" width='"25'" valign='"top'" class='"objList'">";
                //if(strcmp($_SESSION['mntvm_edit'],"1") == 0 || strcmp($_SESSION['mntvm_delete'],"1") == 0)
                {
                    echo "<img id='"".$guid_array[$i]."'" src='"images/icon-reports.gif'" alt='"Options'" title='"Options'" width='"25'" height='"20'" border='"0'">";
                }
                echo "</td>";
                echo "<td class='"objList'" value='"".$lastname_array[$i]."'" width='"'">".$lastname_array[$i]."</td>";             
                echo "<td class='"objList'" value='"".$firstname_array[$i]."'" width='"'">".$firstname_array[$i]."</td>";   
                echo "<td class='"objList'" value='"".$osname_array[$i]."'" width='"'">".$osname_array[$i]."</td>";
                echo "<td class='"objList'" value='"".$cptype_array[$i]."'" width='"'">".$cptype_array[$i]."</td>";
                echo "<td class='"objList'" value='"".$cpcycle_array[$i]."'" width='"'">".$cpcycle_array[$i]."</td>";
                echo "<td class='"objList'" value='"".$tom_array[$i]."'" width='"'">".$tom_array[$i]."</td>";
                echo "</tr>";                   
            }
            ?>
      </table>  
</div>
function kb_to_mb ($kb) {
  return $kb / 1024;
}

…然后…

...width='"'">".kb_to_mb($tom_array[$i])."</td>"

…或者只是…

...width='"'">".($tom_array[$i] / 1024)."</td>"

1kb = 1024bytes
1mb = 1024kb
1gb = 1024mb
...等等

您可能希望将结果round()到小数点后1-2位。

从上面的注释来看,从KB到MB…

$MB = $numKBs / 1024

我还没有测试过,但这应该可以工作。修改这一行:

echo "<td class='"objList'" value='"".$tom_array[$i]."'" width='"'">".$tom_array[$i]."</td>";

:

echo "<td class='"objList'" value='"".round($tom_array[$i] / 1024, 2)."'" width='"'">".round($tom_array[$i] / 1024, 2)."</td>";

这将输出兆字节数,四舍五入到小数点后2位