php - How do I return value right in my HTML Page?
I've been looking at other questions related to my problem however none seem to correspond to my exact circumstances.. I'm doing a TopTen Ranking with some datas about Webpages - Like whats the most IP's visiting your page, when was the last access, how many bytes... all this :)
now i have written a code that gives me all the bytes of the most 10 visitors of the page back.. and i want to return this result to my html page - that works but the output isnt really how i wanted it
this is my code:
$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);
now i returned this to my html page..
the output of $mb_bytes is :
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 )
but i dont want the output in my html be written in a row.. i want it like:
1.56
0.61
0.24
0.16
....
does anyone knows a solution for that? the part in my HTML looks like this:
<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
but i think the foreach in the HTML will do some problems too.. ( I'm working with the LARAVEL Framework
thanks :)
EDIT : now it looks like this !
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
.......
but it should look like this:
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
new edit:
my code :
$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
error message: array_shift() expects parameter 1 to be array, null given
Answer
Solution:
You can try to first preprocess in controller your two
$topTenIp
and$mb_byte
variables combine them in one single array$resultArr
keep there for each item its'ip'
and'byte'
then make loop over $resultArr array like thisand you will have your output like this
UPDATE
you can combine your 2 variables with simple php logic
IMPORTANT if your
count($topTenIp)
==count($mb_byte)
then you can do this to combination themthen pass and use $resultArr in template
Answer
Solution:
try to assign the value to a variable not an array entry.