【问题标题】:How do I return value right in my HTML Page?如何在我的 HTML 页面中返回值?
【发布时间】: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和字节数一样吗?
  • 我将编辑我的问题
  • 您没有在 foreach html 部分中打开/关闭 &lt;?php 标记。这也是非常不寻常的语法。为什么你有@符号?为什么要使用双花括号?
  • @Novocaine — 来自“我正在使用 LARAVEL 框架”的问题,请参阅 laravel.com/docs/5.0/templates

标签: php html arrays laravel


【解决方案1】:

您可以尝试首先在控制器中预处理您的两个$topTenIp$mb_byte 变量将它们组合在一个数组中$resultArr 为每个项目保留其'ip''byte' 然后循环遍历$resultArr 数组像这样

@foreach($resultArr as $item)
   <tr>
      <td> {{ $item['ip'] }} </td>
      <td> {{ $item['byte'] }} </td>
   </tr>
@endforeach

你会得到这样的输出

IP 1.56
IP 0.61
...

更新

您可以将 2 个变量与简单的 php 逻辑结合起来

重要如果你的 count($topTenIp) == count($mb_byte) 那么你可以这样做来组合它们

$resultArr = array();
foreach($topTenIp as $ip){
   $byte = array_shift($mb_byte);
   $resultArr = array('id' => $ip, 'byte' => $byte);
}

然后在模板中传递并使用$resultArr

【讨论】:

  • 我想我明白你的意思.. 但是我该如何结合它们呢?我还在学习php:/
  • 一开始我想将它们结合起来,因为这样对我来说更容易使用,但我不知道如何..
  • 我试过了..但我认为我在控制器和返回视图中做错了..我会尝试一些事情,如果我得到它会写信给你:)
  • 或者如果我没有做到:D
  • 我不知道我做错了什么,但这没有用.. 它只是说:htmlentities() 期望参数 1 是字符串,给定数组(查看:/var/www/laravel/logs/resources /views/domains/data.blade.php) ---- 你能再解释一下我需要在控制器中做什么以及如何在 HTML 中使用它吗?我以为我理解它,但它对我没有用..
【解决方案2】:

尝试将值分配给变量而不是数组条目。

@foreach($topTenIp as $ip)
            <tr>
                <th>{{ $ip }}</th>
                <th> here should be the $mb_byte variable </th>
            </tr>
        @endforeach

【讨论】:

  • 如何将其从数组更改为变量?如果我改变它,变量将是一个字符串,输出是否相同?不好意思,我还在学php……
  • np。在您的 foreach 中,您将 $topTenIp 的每个条目传递给 $ip[$i] $ip <th>{{ $ip }}</th> 一样,这是一个您可以回显的字符串。希望这会有所帮助
  • 也应该是$mb_bytes[$i]?
猜你喜欢
  • 1970-01-01
  • 2022-03-07
  • 1970-01-01
  • 1970-01-01
  • 2014-06-15
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
相关资源
最近更新 更多