Hello can someone help me parse a file into the format i nee

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sublimenal
Forum Newbie
Posts: 23
Joined: Sun May 16, 2004 2:17 pm
Location: Massachusetts

Hello can someone help me parse a file into the format i nee

Post by sublimenal »

]Hi guys great site you got here, im wondering if someone can give me a hand. im trying to set up stats for a game i play. i have a game that outputs stats to a file called stats.ini i want to take this data and put it in tables. i found out kinda how to do this but it takes everything! and i dont want everything i just want like the top 10 or something. here is the code im using now

Code: Select all

<html>
<body bgcolor="#000000" text="#FF8a00">
<? 
// file() makes an array of each line of the file 
$players = file("/home/council/public_html/stats/stats.ini"); 

echo "<table border=1 cellspacing=1 cellpadding=1>"; 

// loop through each line 
foreach($players as $each)&#123; 

  // explode line into an array 
  $line = explode(",",$each); 
  echo "<tr><td>".$line&#1111;0]."</td><td>".$line&#1111;1]."</td><td>".$line&#1111;2]."</td></tr>\n"; 
&#125; 

echo "</table>";

and it displays this

http://www.councilofvalhalla.com/stats/test.php

here is the data file
http://www.councilofvalhalla.com/stats/


if there is anyone that could help me it would be most appreciated.thanks
User avatar
maqmus
Forum Commoner
Posts: 30
Joined: Mon Mar 08, 2004 1:10 pm

Post by maqmus »

I didn't try it but could be something like this:


Code: Select all

<?php

$players = file("/home/council/public_html/stats/stats.ini"); 

echo "<table border=1 cellspacing=1 cellpadding=1>"; 

for($i=0;$i<10;$i++) {
$line = explode(",",$players[$i]); 
  echo "<tr><td>".$line[0]."</td><td>".$line[1]."</td><td>".$line[2]."</td></tr>\n"; 
}

echo "</table>";
?>
sublimenal
Forum Newbie
Posts: 23
Joined: Sun May 16, 2004 2:17 pm
Location: Massachusetts

Post by sublimenal »

thats getting me closer that displays this

http://www.councilofvalhalla.com/stats/test2.php

that script u gave me just displays the top 10 lines, that ini is not in order
it lists like this

Names
Kills
Deaths
Points


so number 1 names you would have to scroll down to kills to see how many kills number 1 got and same for deaths and points.

i want to put this in order by most points or display the top ranking ones
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

This could be done ALOT more effiently and a lot more customizable,
(order by kills, points, whatever ) if you used a database.
sublimenal
Forum Newbie
Posts: 23
Joined: Sun May 16, 2004 2:17 pm
Location: Massachusetts

Post by sublimenal »

that would be fine but i need a script that would insert it into a database becuase thats the way the stats.ini comes out. and ideas?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I've been trying for the past 30 min, I'm about to give up :S, this would be so easy in a database.

edit: out of curiosity does the amount of each field ( names, kills, etc, ) change?
sublimenal
Forum Newbie
Posts: 23
Joined: Sun May 16, 2004 2:17 pm
Location: Massachusetts

Post by sublimenal »

wow i cant believe ur trying i really appreciate what your doing.

The names field always stays the same but the other fields change. becuase say number 1 got 3 kills one game and 3 kills another. if you look under number 1 for kills he would have 6
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

<?php 

$line = file("stats.ini"); 

echo "<table>
          <tr><td>Player Name</td>
          <td>Kills      </td>
          <td>Deaths     </td>
          <td>Points     </td>
	  </tr>";

for($i=1;$i<=10;$i++) 
{ 
$name = $line[0+$i];
$name = substr($name, 9); 

$kill = $line[500+$i];
$kill = substr($kill, 9);
 
$death = $line[1000+$i];
$death = substr($death, 10);

$points = $line[1000+$i];
$points = substr($points, 10);

echo "<tr><td>".$name."</td>
                <td>".$kill."</td>
                <td>".$death."</td>
                <td>".$points."</td>
         </tr>";

}

?>


Once Again I recommend you don't use this, as it's really not that well written, I'm sort of in the middle of brain bubble, and anxious to ge to the bar :P. Use this as a reference and try and make this more efficient.
sublimenal
Forum Newbie
Posts: 23
Joined: Sun May 16, 2004 2:17 pm
Location: Massachusetts

Post by sublimenal »

dude thats awesome what u did! im almost there! jeez you guys are amazing
//
//Gives cookie to all you programmers
//

now that takes my first 10 people the only thing is, its not really telling me the top 10 its just pulling the top 10 people on the list someone can join my server and be number 49 but have the most kills and he wont be in that list . is there a way to organize it by high points? or maby there is a way i can put this all into a mysql database? im not sure but im almost there. any more suggestions?
sublimenal
Forum Newbie
Posts: 23
Joined: Sun May 16, 2004 2:17 pm
Location: Massachusetts

Post by sublimenal »

btw if i can ask another question how do i make it so i can display all names? instead of 10, i know i asked for this but i just want to look
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Hold on theres another problem with the script, let me re-work it a little so help you display all the players.

*edit* thinks back to the mysql possibility...
K what you have to do is create a table called stats

stats table will look like this

PLAYER NAME || KILLS || DEATHS || POINTS
=============================
player 1 || 0 || 0 || 0
player 2 || 0 || 0 || 0
player 3 || 0 || 0 || 0
player 4 || 0 || 0 || 0


let me work the script for you...
sublimenal
Forum Newbie
Posts: 23
Joined: Sun May 16, 2004 2:17 pm
Location: Massachusetts

Post by sublimenal »

ok awesome, you are the man! the whole rune community will thank you for this lol!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

<?php

$line = file("stats.ini");

echo "<table>
<tr><td>Player Name</td>
<td>Kills </td>
<td>Deaths </td>
<td>Points </td>
</tr>";

for($i=1;$i<=500;$i++)
{

$name = $line[0+$i];
$kill = $line[500+$i];
$death = $line[1000+$i];
$points = $line[1000+$i];

if ($i < 10)
{
$name = substr($name, 9);
$kill = substr($kill, 9);
$death = substr($death, 10);
$points = substr($points, 10);
}
elseif ($i < 100)
{
$name = substr($name, 10);
$kill = substr($kill, 10);
$death = substr($death, 11);
$points = substr($points, 11);
}
elseif ($i > 99)
{
$name = substr($name, 11);
$kill = substr($kill, 11);
$death = substr($death, 12);
$points = substr($points, 12);
}

if (($name != "") && ($points != 0))
{

echo "<tr><td>".$name."</td>
<td>".$kill."</td>
<td>".$death."</td>
<td>".$points."</td>
</tr>";

}
}
?>

Code: Select all

<?php

?>
This is what I got so far.. now it's time to configure this script to add all the information into the database...

so far this script will list all the players taht have more than 0 points.

Once we configure this for a database we can do anything very easily.. only changing a few lines of code... like we can order them from highest point or kills or anything.. even make links that u click on and it changes the order...

BTW: whats rune?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

<?php 

$line = file("stats.ini"); 

echo "<table>
          <tr><td>Player Name</td>
          <td>Kills      </td>
          <td>Deaths     </td>
          <td>Points     </td>
	  </tr>";

for($i=1;$i<=500;$i++) 
{ 

	$name   = $line[0+$i];
	$kill   = $line[500+$i];
	$death  = $line[1000+$i];
	$points = $line[1000+$i];

	if ($i < 10)
		{
		$name   = substr($name, 9); 
		$kill   = substr($kill, 9);
		$death  = substr($death, 10);
		$points = substr($points, 10);
  	    }
	elseif ($i < 100)
		{
		$name   = substr($name, 10); 
		$kill   = substr($kill, 10);
		$death  = substr($death, 11);
		$points = substr($points, 11);
 	    }
	elseif ($i > 99)
		{
		$name   = substr($name, 11); 
		$kill   = substr($kill, 11);
		$death  = substr($death, 12);
		$points = substr($points, 12);
 	    }	
    
if (($name != "") && ($points != 0))
	{ 

echo "<tr><td>".$name."</td>
          <td>".$kill."</td>
          <td>".$death."</td>
	      <td>".$points."</td>
	  </tr>";
	
	}
} 


?>
This is what I got so far.. now it's time to configure this script to add all the information into the database...

so far this script will list all the players taht have more than 0 points.

Once we configure this for a database we can do anything very easily.. only changing a few lines of code... like we can order them from highest point or kills or anything.. even make links that u click on and it changes the order...

BTW: whats rune?
sublimenal
Forum Newbie
Posts: 23
Joined: Sun May 16, 2004 2:17 pm
Location: Massachusetts

Post by sublimenal »

awesome im so excited you dont even know lol...


rune is a game that uses the unreal engine but instead of guns you get swords, hammers, and axes. the game is nuts! its kinda old like 4-5 years old but great graphics. i will happily mail you or send you a copy online since all the work u did for me.
Post Reply