3

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.

7
  • 1
    Have you tried something ? To get the data from the file? Commented Apr 26, 2015 at 19:02
  • I know how to get data from a file using php, but i dont really know how to organize every username with his score in table, im lost Commented Apr 26, 2015 at 19:03
  • 1
    ^ Then show your effort and your work which you have done! Include it into your question! Commented Apr 26, 2015 at 19:04
  • Just edit your question an add your attempt Commented Apr 26, 2015 at 19:10
  • zer0fl4g's method has worked for me. It's simple and fast therefore I have chosen his answer :) thank you all Commented Apr 26, 2015 at 19:37

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

3 Comments

Update: I tested it and it's working perfectly. Thanks
@SeifHassine You're welcome! (So I'm going to assume you actually wanted the table to be sorted?)
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
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.

1 Comment

@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
2

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.