Getting data from text file and display it in html table

22,229

Solution 1

This should work for you:

Here I first get all lines into an array with file() where every line is one array element. There I ignore empty lines and new line characters at the end of each line.

After this I go through each line with array_map() and extract the username + score with explode(), which I then return as array to create a multidimensional array, e.g:

Array
(
    [0] => Array
        (
            [username] => a
            [score] => 5
        )
    //...

The I sort the array by the score with usort() (To change the order from ASC to DESC you can just change > to < in usort()) and after this I simply loop through the data and print it in the table.

<?php 

    $lines = file("scores.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    $data = array_map(function($v){
        list($username, $score) = explode(":", $v);
        return ["username" => $username, "score" => $score];
    }, $lines);

    usort($data, function($a, $b){
        if($a["score"] == $b["score"])
            return 0;
        return $a["score"] > $b["score"] ? 1 : -1;
    });

?>

<table width="200" border="1">
    <tr>
        <td width="85">Nom</td>
        <td width="99">Score</td>
    </tr>
<?php foreach($data as $user){ ?>
    <tr>
        <td height="119"><?php echo $user["username"]; ?></td>
        <td><?php echo $user["score"]; ?></td>
    </tr>
<?php } ?>
</table>

output:

Nom   Score  // Nom   Score
 e      2    //  d     123
 a      5    //  c     26
 b     15    //  b     15
 c     26    //  a      5
 d    123    //  e      2

Solution 2

Sample Text File Data

user1 : 100
user2 : 80
user3 : 60
user4 user4 : 75

PHP Code

<table width="200" border="1">
    <tr>
        <td width="85">Nom</td>
        <td width="99">Score</td>
    </tr>
<?php
$file_handle = fopen("sample.txt", "rb");

while (!feof($file_handle) ) {
    $line_of_text = fgets($file_handle);
    $parts = explode(':', $line_of_text);
    echo "<tr><td height='119'>$parts[0]</td><td>$parts[1]</td></tr>";
}
fclose($file_handle);
?>
</table>

Result

To solve your problem to separate text between ":", u can use explode function. explode function require two parameter. The first one is the character you want to seaparate(delimiter), and the second is the string you want to separate. Example like yours is text file. You must read the text file line by line. So you can use fgets() function and explode that line. Hope this will help you understand.

Solution 3

Rizier123's answer was perfect. But I had to make some changes to get it to work in my system.

<!DOCTYPE HTML>
<html>
<body>
<?php

    $lines = file("scores.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    function explodeLine($v)
    {
        list($username, $score) = explode(":", $v);
        return array("username" => $username, "score" => $score);
    }
    $data = array_map('explodeLine', $lines);

    function comparatorFunc($a, $b)
    {
        if($a["score"] == $b["score"])
            return 0;
        return $a["score"] > $b["score"] ? 1 : -1;
    }
    usort($data, 'comparatorFunc');

?>

<table width="200" border="1">
    <tr>
        <td width="85">Nom</td>
        <td width="99">Score</td>
    </tr>
<?php foreach($data as $user){ ?>
    <tr>
        <td height="119"><?php echo $user["username"]; ?></td>
        <td><?php echo $user["score"]; ?></td>
    </tr>
<?php } ?>
</table>
</body>
</html>
Share:
22,229
Seif Hassine
Author by

Seif Hassine

Updated on July 09, 2022

Comments

  • Seif Hassine
    Seif Hassine almost 2 years

    I got a text file in this pattern for each line:

    Username : Score
    

    I'm trying to create a scoreboard out of this.

    This is my attempt:

    <table width="200" border="1">
      <tr>
        <td width="85">Nom</td>
        <td width="99">Score</td>
      </tr>
      <tr>
        <td height="119"></td>
        <td></td>
      </tr>
    </table>

    The question is how can I copy each username and score to the table (without the : character) ?

    EDIT:

    My current php code:

    <?php 
    
        $file = file_get_contents('facile.txt', true);
        $arr = explode("/", $file, 2);
        $first = $arr[0];
    
    ?>
    

    This will give me only the first username, but I want to get all the usernames from every line.

  • Rizier123
    Rizier123 about 9 years
    @zer0fl4g Only because an answer is helpful doesn't mean that OP has to accept your answer (Maybe upV it, because that's also what the tooltip says if you hover over the upV arrow)! There is a good link which helps to decide what answer OP can/should accept: meta.stackexchange.com/q/5234
  • Seif Hassine
    Seif Hassine about 9 years
    Update: I tested it and it's working perfectly. Thanks
  • Rizier123
    Rizier123 about 9 years
    @SeifHassine You're welcome! (So I'm going to assume you actually wanted the table to be sorted?)
  • Seif Hassine
    Seif Hassine about 9 years
    Ah, at first I just wanted to directly transfer data from file to table, but it's always better to sort usernames by scores, that's why I chose your answer