【发布时间】:2016-03-15 20:09:26
【问题描述】:
我一直在查看与我的问题相关的其他问题,但似乎没有一个与我的确切情况相对应。我正在使用一些关于网页的数据进行 TopTen 排名 - 比如访问您页面的最多 IP 是什么时候最后一次访问,多少字节......所有这些:)
现在我已经编写了一个代码,它可以将页面的最多 10 位访问者的所有字节返回给我。我想将此结果返回到我的 html 页面 - 可以,但输出并不是我想要的那样
这是我的代码:
$mb_bytes = array();
foreach ($topTenIp as $val) {
$bytes_ips[$val] = shell_exec("grep $val /path/file.log | awk '{print $10}'");
$add = array_sum(explode("\n", $bytes_ips[$val]));
$add = $add / 1024 / 1024;
$add = round($add, 2);
array_push($mb_bytes, $add);
}
if($mb_bytes)
arsort($mb_bytes);
现在我将此返回到我的 html 页面..
$mb_bytes 的输出是:
Array ( [0] => 1.56 [5] => 0.61 [4] => 0.24 [1] => 0.16 [3] => 0.13 [2] => 0.08 [9] => 0.01 [6] => 0.01 [7] => 0.01 [8] => 0 )
但我不希望我的 html 中的输出连续写入.. 我希望它像:
1.56
0.61
0.24
0.16
....
有人知道解决方案吗?我的 HTML 中的部分如下所示:
<tr>
<th> Top 10</th>
<th> Bytes</th>
<th> Date </th>
</tr>
@foreach($topTenIp as $ip[$i])
<tr>
<th>{{ $ip[$i] }}</th>
<th> here should be the $mb_byte variable </th>
</tr>
@endforeach
但我认为 HTML 中的 foreach 也会出现一些问题。(我正在使用 LARAVEL 框架
谢谢:)
编辑:现在看起来像这样!
IP 1.56 0.61 0.24 0.16 0.13 0.08 0.01 0.01 0.01 0
IP 1.56 0.61 0.24 0.16 0.13 0.08 0.01 0.01 0.01 0
IP 1.56 0.61 0.24 0.16 0.13 0.08 0.01 0.01 0.01 0
.......
但它应该是这样的:
IP 1.56
IP 0.61
IP 0.24
IP 0.16
IP 0.13
IP 0.08
IP 0.01
IP 0.01
IP 0.01
IP 0
新编辑:
我的代码:
$mb_bytes = array();
foreach ($topTenIp as $val) {
$bytes_ips[$val] = shell_exec("grep $val /path/file | awk '{print $10}'");
$add = array_sum(explode("\n", $bytes_ips[$val]));
$add = $add / 1024 / 1024;
$add = round($add, 2);
array_push($mb_bytes, $add);
}
if($mb_bytes)
arsort($mb_bytes);
$resultArr = array();
foreach($topTenIp as $ip){
$byte = array_shift($mb_byte);
$resultArr = array('id' => $ip, 'byte' => $byte);
}
html:
@foreach($resultArr as $item)
<tr>
<td> {{ $item['ip'] }} </td>
<td> {{ $item['byte'] }} </td>
</tr>
@endforeach
错误信息:array_shift() 期望参数 1 为数组,给定为空
【问题讨论】:
-
你能告诉我你有什么问题吗?
-
你的ip和字节数一样吗?
-
我将编辑我的问题
-
您没有在
foreachhtml 部分中打开/关闭<?php标记。这也是非常不寻常的语法。为什么你有@符号?为什么要使用双花括号? -
@Novocaine — 来自“我正在使用 LARAVEL 框架”的问题,请参阅 laravel.com/docs/5.0/templates