php - How do I return value right in my HTML Page?

525

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

959

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 this

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

and you will have your output like this

IP 1.56
IP 0.61
...

UPDATE

you can combine your 2 variables with simple php logic

IMPORTANT if yourcount($topTenIp) ==count($mb_byte) then you can do this to combination them

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

then pass and use $resultArr in template

7

Answer

Solution:

try to assign the value to a variable not an array entry.

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

People are also looking for solutions to the problem: php - Easiest way to get birthday info of all friends through graph api?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.