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

Php 如何以MB为单位显示总大小,php,Php,只是想知道是否有人能帮我尽快解决这个问题 我有一个php脚本,它工作正常,但是,我希望它以MB而不是kb显示总内存 <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_U

只是想知道是否有人能帮我尽快解决这个问题

我有一个php脚本,它工作正常,但是,我希望它以MB而不是kb显示
总内存

<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>
VM列表

根据上面的评论,从KB到MB

$MB = $numKBs / 1024
……然后

...width=\"\">".kb_to_mb($tom_array[$i])."</td>"
…宽度=\“\”>“.kb\u到\u mb($tom\u数组[$i])”
…或者只是

...width=\"\">".($tom_array[$i] / 1024)."</td>"
…宽度=\“\”>“($tom\u数组[$i]/1024)。”
1 KB=1024字节
1 MB=1024 KB
1 GB=1024 MB
…等等,等等


您可能希望将结果保留到小数点后1-2位。

我还没有测试过这个,但应该可以。更改此行:

echo "<td class=\"objList\" value=\"".$tom_array[$i]."\" width=\"\">".$tom_array[$i]."</td>";
echo“$tom_数组[$i]”;
为此:

echo "<td class=\"objList\" value=\"".round($tom_array[$i] / 1024, 2)."\" width=\"\">".round($tom_array[$i] / 1024, 2)."</td>";
echo”“。圆形($tom_数组[$i]/1024,2)。”;

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

数据库中保存的总内存是多少?目前存储的内存是多少?字节数?@DaveRandom:)问题编辑
…宽度=\“\”>。舍入($tom\u数组[$i]/1024)。“
-舍入到最接近的整数,向下舍入,向上舍入。:)是的,我能做到,非常感谢
echo "<td class=\"objList\" value=\"".round($tom_array[$i] / 1024, 2)."\" width=\"\">".round($tom_array[$i] / 1024, 2)."</td>";