Home > Blockchain >  How i can make the last line from the pucture in PHP?
How i can make the last line from the pucture in PHP?

Time:10-27

That's the code I've tried so far , basically I have file from which I've displayed the info in the table.But i can't understand how to make the last line from the table. That's how the table should look like: table

<?php

    $file = fopen('name.txt', "r");

    echo "<table border \"1px\">";
    echo "<tr>
                    <th>Name</th>
                    <th>Salary</th>
                    <th>Town</th>
                    <th>Age</th>
                <tr>";


    while (!feof($file)) {
        $string = fgets($file);
        $finalArray = array();

        $asArr = explode(',', $string);
        foreach ($asArr as $val) {
            $tmp = explode(':', $val);
            $finalArray[$tmp[0]] = $tmp[1] ?? "";
        }
        $cols = array_values($finalArray);
        echo '<tr>';
        foreach ($cols as $col) echo '<td>' . $col . '</td>';
        echo '</tr>';

        foreach ($finalArray[$cols[0]] as $i => $null) {
            echo '<tr>';
            foreach ($cols as $col) echo '<td>' . $day[$col][$i] . '</td>';
            echo '</tr>';
        }
    }

    echo "</table>";


    ?>

Those are the contents of the file:

name:Ivan,Salary:5000,town:Sofia,age:20
name:Pesho,Salary:1500,town:Pleven,age:19
name:Gosho,Salary:2000,town:Varna,age:18
name:Georgi,Salary:3000,town:Pleven,age:46
name:Ivailo,Salary:6000,town:Pleven ,age:25
name:Stamat,Salary:7000,town:Varna,age:46
name:Aleksandar,Salary:1500,town:Burgas,age:44
name:Kiko,Salary:5000,town:Plovdiv,age:25
name:Misho,Salary:5250,town:Sofia,age:24
name:Daniel,Salary:3000,town:Plovdiv,age:34
name:John,Salary:6000,town:Pleven,age:50
name:Ana,Salary:9000,town:Sofia,age:18
name:Maria,Salary:9500,town:Sofia,age:30
name:Marian,Salary:9500,town:Sofia,age:20
name:Petko,Salary:9500,town:Sofia,age:19
name:Nikola,Salary:9500,town:Sofia,age:45
name:Ani,Salary:9500,town:Sofia,age:47


CodePudding user response:

This code will display table like this https://i.imgur.com/GZKFzsf.png

<?php

$file = fopen('name.txt', "r");
$names = [];
$cities = [];
$salaries = [];
$ages = [];
echo "<table border \"1px\">";
echo "<tr><th>Name</th><th>Salary</th><th>Town</th><th>Age</th> </tr>";
while (!feof($file)) {
    $string = fgets($file);
    $finalArray = [];

    $asArr = explode(',', $string);
    foreach ($asArr as $val) {
        $tmp = explode(':', $val);
        $finalArray[$tmp[0]] = $tmp[1] ? trim($tmp[1]) : "";
    }
    $cols = array_values($finalArray);
    [$name, $salary, $city, $age] = $cols;
    $names[] = $name;
    $salaries[] = $salary;
    $cities[] = $city;
    $ages[] = $age;
    echo '<tr>';
    foreach ($cols as $col) {
        echo '<td>' . $col . '</td>';
    }
    echo '</tr>';

    $something = $cols[0];
}
$countOfNames = count(array_unique($names));
$citiesCount = array_count_values($cities);
$maxVal = max($citiesCount);
$mostCommonCity = array_search($maxVal, $citiesCount);
$sumOnAllSalary = array_sum($salaries);
$averageAges = (int)(array_sum($ages) / count($ages));
echo '<tr>';
echo '<td>' . $countOfNames . '</td>';
echo '<td>' . $sumOnAllSalary . '</td>';
echo '<td>' . $mostCommonCity . '</td>';
echo '<td>' . $averageAges . '</td>';
echo '</tr>';
echo "</table>";
  •  Tags:  
  • php
  • Related