LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Parsing a text file to display certain data on my website in php? (https://www.linuxquestions.org/questions/linux-software-2/parsing-a-text-file-to-display-certain-data-on-my-website-in-php-778264/)

jumperbee 12-26-2009 11:32 PM

Parsing a text file to display certain data on my website in php?
 
Hi! ^^

Im trying to figure out how to display parts of a .db file created by the scorch2000 server to display a player name, games played, score and maybe more...

here is the text file format example:

playername1 password email #ofgames score setting1 setting2 setting3
playername2 password email #ofgames score setting1 setting2 setting3
playername3 password email #ofgames score setting1 setting2 setting3
playername4 password email #ofgames score setting1 setting2 setting3
playername5 password email #ofgames score setting1 setting2 setting3

I dont want to display everything, of course ^^, but how do i get the player name, the number of games he played and his score to display it in a webpage in this fasion:

Name Games Played Score

joe blow1 25 9876890
joe blow2 31 8989767
joe blow2 26 7989767
joe blow2 17 5989767
joe blow2 13 4989767

and by highest score because the log doesin't put them in in score order.... :(

please help, i asked the maker because he has one runing already but no answer back, well the game is pretty old so i didn't really expect an answer anyways and tryed to figure it out but i dont know functions in php, this is to include in a php-nuke block (this i know how to do :)

here is an example of a working page at the developper website:
http://scorch2000.com/top100.html

thank you

ammorais 12-27-2009 04:11 PM

I'll send you the bill later.

PHP Code:

<table border="1" borderColor="black" cellSpacing="0" cellPadding="5">
<tr bgcolor="#aaaaaa"><TH>playername1</TH><TH>password</TH><TH>email</TH><TH>#ofgames</TH><TH>score</TH><TH>setting1</TH><TH>setting2</TH><th>setting3</th></TR>

<?php
    $handle 
fopen("file.db""r");
    
    if (
$handle) {
    
        while (!
feof($handle)) {
            
$buffer fgets($handle4096);
            
$line_arr explode(' '$buffer);
            
            echo 
"<tr>";
            foreach(
$line_arr as $value)
            {
                echo 
"<td>$value</td>";
            }
            echo 
"</tr>";            
        }
    }
?>
</table>


jumperbee 12-27-2009 05:03 PM

^^ thanks!
 
Seriously if you have a paypal account it would be my pleasure to give you a little something :) , im not rich but i'll give you what i can....

you can send your info here admin@excelclan.com if you wish,, like i said it would be a pleasure :) because this is actually the first code i know that is tailor made for this classic game server so i thank you VERY much and is very much appreciated ^^

Thanks again

jumperbee 12-27-2009 05:06 PM

Woooooooooooooooow this looks nice! hihihihi ^^

jumperbee 12-27-2009 05:21 PM

1 Attachment(s)
Quote:

Originally Posted by ammorais (Post 3806149)
I'll send you the bill later.

PHP Code:

<table border="1" borderColor="black" cellSpacing="0" cellPadding="5">
<tr bgcolor="#aaaaaa"><TH>playername1</TH><TH>password</TH><TH>email</TH><TH>#ofgames</TH><TH>score</TH><TH>setting1</TH><TH>setting2</TH><th>setting3</th></TR>

<?php
    $handle 
fopen("file.db""r");
    
    if (
$handle) {
    
        while (!
feof($handle)) {
            
$buffer fgets($handle4096);
            
$line_arr explode(' '$buffer);
            
            echo 
"<tr>";
            foreach(
$line_arr as $value)
            {
                echo 
"<td>$value</td>";
            }
            echo 
"</tr>";            
        }
    }
?>
</table>




This works great but puts all of the line in playername1,, how do i display only the player name in that field, then in the next the numbers of games he player then after that the score in the next field? or how does the script work?

sorry about that :( here is what it looks like http://www.excelclan.info/games/scor...layerstats.php

Also i cant copy paste the players.db file because it only copies the first word of the file in notepad++ because of the NUL symbols (dont know why) so i have attached a picture of the file....

jumperbee 12-27-2009 06:10 PM

ok i figured out that $line_arr = explode(' ', $buffer); this is the value that takes in to account that after the space it becomes the next value, but in this log file there is a NUL symbol, what is this?, what can i put between the quotes in $line_arr = explode('HERE', $buffer); to represent that NUL symbol?

jumperbee 12-27-2009 06:39 PM

ok
 
ok the problem is that the server removes those NUL symbols and i loose the spaces,, is there a way to read that file with this script while keeping the spaces?

or can i copy this file to the location of this script, covert it to another format and read it,, but how or with what do i convert it?, i know how to copy it with cron and all but what program on an ubuntu server can i use to convert this file to unicode or a format wich keeps the spaces?

thanks

ammorais 12-27-2009 06:57 PM

What is the format of the db file?
I use the function explode(' ', $buffer) that separates the words by space.
For example. The line
Code:

something1 this is a phrase something2
will be converted to an array of 5 words
Code:

something1
this
is
a
phrase
something2

If you want the spaces the db file must have a diferent format. For example like this:
Code:

something1,this is a phrase,something2
Then change the
PHP Code:

explode(' '$buffer

to
PHP Code:

explode(','$buffer

The result will be an array with 3 words
Code:

something1
this is a phrase
something2

For more info have a look at explode documentation

It's also possible that the db file separates phrases with tabs. If that's the case simple change the explode line to

PHP Code:

explode("\t"$buffer); 

[EDIT]

You mentioned the NUL symbol:
Try:
PHP Code:

explode("\0"$buffer

[/EDIT]

jumperbee 12-27-2009 07:16 PM

1 Attachment(s)
Notepad++ tells me that this is an ANSI Windows format, it is generated by the game server wich uses java.

I tryed the "\0" found that out on wikipedia but still shows the same thing... all together... also tryed with your suggested "\t" (tabs), no go :(

ammorais 12-27-2009 07:21 PM

can you zip it and atatch it?

jumperbee 12-27-2009 07:24 PM

I attached it to my last post,, i changed the extension to txt from db,, but if you open it with notepad++ you see the NUL symbols....

thank you very much ^^

jumperbee 12-27-2009 07:37 PM

here it is in a zip format with the extention .db, arff cant attach zip files.... but i posted it in txt format above...

ammorais 12-27-2009 07:42 PM

Ok. Let me check

ammorais 12-27-2009 07:50 PM

Ok. The end of line is 0x0A and the words are separated by 0x00

I also sorted the results by Score

PHP Code:

<TABLE border="1" borderColor="black" cellSpacing="0" cellPadding="5">
<TR bgcolor="#aaaaaa"><TH>playername1</TH><TH>password</TH><TH>email</TH><TH>#ofgames</TH><TH>score</TH><TH>setting1</TH><TH>setting2</TH><th>setting3</th></TR>

<?php
    $filename 
"players.txt";
    
$handle fopen($filename"r");
    
$multi_arr = array();
    if (
$handle) {
    
        
$buffer fread($handlefilesize($filename));
        
$lines explode(chr(10), $buffer);
        foreach(
$lines as $line)
        {

            
$multi_arr[] = explode(chr(0), $line);        
        }        
    }
    
$sort_ = array();
    foreach(
$multi_arr as  $value) {
            
$sort_[] = $value[4];
    }
        
    
array_multisort($sort_SORT_DESCSORT_NUMERIC$multi_arr);
    foreach(
$multi_arr as $value1)
    {
        echo 
"<tr>";
        foreach(
$value1 as $value2)
        {
            echo 
"<td>$value2</td>";
        }
        echo 
"</tr>";
    }
?>
</table>


jumperbee 12-27-2009 07:52 PM

kewwwwlll!,, ok i checked the link you provided on the explode() argument but i cant see anything that explains on how to print only chunks of a line, for example mine has 14 values,, but how do i only print the name, the numbers of games played and the score?, because the other values are passwords and e-mails i wouldn't want to display that to the public.....

if you dont feel like explaining can you provide me with the argument i can use to do this?


All times are GMT -5. The time now is 10:18 AM.