只是想知道是否有人可以快速帮助我。
我有一个PHP脚本它运行正常,但是,我希望它以MB为单位显示Total Memory
而不是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>
答案 0 :(得分:1)
function kb_to_mb ($kb) {
return $kb / 1024;
}
......然后......
...width=\"\">".kb_to_mb($tom_array[$i])."</td>"
......或者只是......
...width=\"\">".($tom_array[$i] / 1024)."</td>"
1 KB = 1024字节
1 MB = 1024 KB
1 GB = 1024 MB
......等等
您可能希望round()
结果为小数点后1-2位。
答案 1 :(得分:0)
从上面的评论中,从KB到MB ......
$MB = $numKBs / 1024
答案 2 :(得分:0)
我没有对此进行测试,但这应该可行。改变这一行:
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>";
这将输出兆字节数,四舍五入到小数点后两位。