Ranking PHP

General discussion for off-topic subjects.
User avatar
Painkiller
Senior Member
Posts: 725
Joined: Sun Mar 01, 2015 8:09 am
Location: Germany
Contact:

Ranking PHP

Postby Painkiller » Sat Dec 31, 2016 12:53 pm

Hi mates, if someone with PHP or html know, he could help me please this the thing again functions.

It's old and it belongs to the mod on my server.

Unfortunately, it does not work and I'm not very experienced in PHP and HTML just a little.

This is how it looks at the moment:
http://hlsw.rocks-clan.de/~akooh/fastDL ... _stats.php


Syntax: Select all

<html>
<head>
<script src="">
</script>
<style>
body {
background-color: black;
color: white;
font-family: "Trebuchet MS", sans-serif;
font_size: 16px;
background-repeat: no-repeat;
background-image: url(http://hlsw.rocks-clan.de/~akooh/fastDL/config/background01.png);
background-attachment:fixed;
text-shadow: 0px 1px 2px black;
}

h1 {
padding: none;
margin: none;
font-size: 24px;
}
h1 em {
font-size: 16px;
font-weight: bold;
display: inline-block;

}
table.sortable tr td:first-child {
text-align: center;
}
table.sortable th:not(.sorttable_sorted):not(.sorttable_sorted_reverse):not(.sorttable_nosort):after {
content: " \25B4\25BE"
}

#details {
float: right;
padding: 0;
margin: 0;
}

#details table {
border-spacing: 0;
border-collapse: collapse;
}

.grid {
display: flex;
flex-direction: row;
}
.grid * {
flex-grow: 1;
width: 50%;
}

.legend {
display: block;
float: left;
width: 10em;
font-weight: bold;
}

#details {
width: 25%;
visibility: hidden;
position: fixed;
right: 1em;
}

#details table th {
text-align: left;
}

#details table h2 {
padding: none;
margin: none;
padding-top: 1.5em;

}

#lang {
display: block;
top: 0;
right: 0;
position: absolute;
width: 24px;
height: 24px;
}

.clickable {
cursor: pointer;
}
</style>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>

<div id="details">
<table>
<tr><td colspan="2"><h1 id="name">Fuckin Fucks</h1><i id="lang"><i>
</td></tr>
<tr><th class="legend">last connected on:</th> <td id="last_connection">13. Juni 2016 11h23</td></tr>
<tr><th class="legend">Total Points:</th><td id="points">12312312</td></tr>
<tr><th class="legend">Maps played:</th> <td id="maps">71</td></tr>
<tr><th class="legend">Jetpacks left:</th> <td id="jet">2</td></tr>

<tr><td colspan="2"><h2>Kills</h2></td></tr>

<tr><th class="legend">Kills:</th> <td id="kills">2130</td></tr>
<tr><th class="legend">Deaths:</th> <td id="deaths">860</td></tr>
<tr><th class="legend">Suicides:</th> <td id="suicides">29</td></tr>

<tr><td colspan="2"><h2>Records</h2></td></tr>
<tr><th class="legend">Consecutive Kills:</th> <td id="consecutive_kills">45</td></tr>
<tr><th class="legend">Max pts for 1 kill:</th> <td id="max_point">1234</td> </tr>

<tr><th class="legend">Distance:</th> <td id="distance">109</td></tr>
<tr><th class="legend">Postal:</th> <td id="postal">6</td></tr>

<tr><td colspan="2"><h2>Naughty</h2></td></tr>
<tr><th class="legend">Punished:</th> <td id="punished">45</td></tr>
<tr><th class="legend">Kicked:</th> <td id="kicks">1234</td></tr>
<tr><th class="legend">Ban:</th> <td id="reason_for_ban"></td></tr>
</table>
</div>

</div>
</table>
</div>
<h1>Ranking

<?php
function joinPaths() {
$args = func_get_args();
$paths = array();
foreach ($args as $arg) {
$paths = array_merge($paths, (array)$arg);
}

$paths = array_map(create_function('$p', 'return trim($p, "/");'), $paths);
$paths = array_filter($paths);
return join('/', $paths);
}


$ftp_connection = array(
"username" => "",
"password" => "",
"server" => "gs.rocks-clan.de",
"port" => "",
# this is probably the same
"directory" => "home/akooh/gameserver/postalalt/hl2mp",
);

$local_directory = "stats/";
$max_age = 5;

$file_names = array(
"rank.txt",
);
$info_file = "stats/index.json";


function updateLastDownload($info_file){

$json_data = array(
"last_download" => time(),
);
//echo $json_data;
$json_string = json_encode($json_data);
//echo $json_string;
file_put_contents($info_file, $json_string);
}

if (!file_exists($info_file)) {
//echo "File was not existing";
updateLastDownload($info_file);
}

$json_string = file_get_contents($info_file);
$json_data = json_decode($json_string, true);
$age_difference = (time() - $json_data["last_download"]);
$file_too_old = $age_difference > (60 * $max_age);

if($file_too_old) {
//echo "Downloading";
$connection = ftp_connect($ftp_connection["server"]);
$login_successful = ftp_login($connection,
$ftp_connection["username"],
$ftp_connection["password"]);

if((!$connection) || (!$login_successful)) {
echo "[[Error Connecting.]]";
exit;
}
foreach ($file_names as $file_name) {
$server_file = joinPaths($ftp_connection["directory"], $file_name);
$local_file = joinPaths($local_directory, $file_name);

$last_load = ftp_get($connection, $local_file, $server_file, FTP_BINARY);
//echo "Dowload of file ". $file_name . " was successful:" . $last_load;
}


ftp_close($connection);
updateLastDownload($info_file);
}
else {
//echo "file is current";
}
$age_difference_minutes = round($age_difference / 60.0, 0);
if ($age_difference_minutes == 0) {
echo "<em>Last Update: Right now !</em>";
} elseif ($age_difference_minutes == 1) {
echo "<em>Last Update: 1 minute ago.</em>";
} else {
echo "<em>Last Update: " . $age_difference_minutes . " minutes ago.</em>";
}
?>
</h1>
<?php

$columns = array(
"steamid" => 0,
"name" => 1,
"kills" => 2,
"deaths" => 3,
"teamkill" => 4,
"kicks" => 5,
"suicides" => 6,
"points" => 7,
"maps" => 8,
"burn" => 9,
"max_point" => 13,
"punished" => 15,
"reason_for_ban" => 21,
"ban" => 22,
"distance" => 16,
"postal" => 29,
"sensei" => 32,
"headshots" => 25,
"lang" => 18,
"consecutive_kills" => 12,
"last_connection" => 26,
"jet" => 17,
);

foreach($file_names as $file_name) {
$local_file = joinPaths($local_directory, $file_name);
$contents = file_get_contents($local_file);
$file = file($local_file);

function breakit($csv_string) {
return str_getcsv($csv_string, ";");
}

$data = array_map("breakit", $file, array(";"));

function compare($a, $b) {
global $columns;
$lhs = $a[$columns["points"]];
$rhs = $b[$columns["points"]];
if ($lhs == $rhs) return 0;
return $lhs > $rhs ? -1 : 1;
}

uasort($data, compare);
$rank = 1;
$header_columns = array(
"name", "points", "kills",
"deaths", "postal", "sensei", "consecutive_kills",
"suicides", "kicks",
);

$detail_columns = array(
"name",
"last_connection",
"points",
"maps",
"jet",
"kills",
"kicks",
"lang",
"deaths",
"suicides",
"consecutive_kills",
"max_point",
"distance",
"postal",
"punished",
"max_point",
"reason_for_ban"
);
echo "<table class='sortable'>";


echo "<tr>";

echo "<th>Rank</th>";
foreach($header_columns as $header) {
echo "<th>" . ucfirst($header) . "</th>";
}
echo "</tr>";
foreach ($data as $userinfo) {
$javascript_thingi = "updateDetails({";
foreach($detail_columns as $detail ) {
$javascript_thingi .= "'$detail': '";
$javascript_thingi .= $userinfo[$columns[$detail]];
$javascript_thingi .= "',";
}
$javascript_thingi .= "});";
echo "<tr class=\"clickable\" onclick=\"$javascript_thingi\">";

echo "<td>$rank</td>";
$rank++;
foreach ($header_columns as $header_column) {
echo "<td>" . $userinfo[$columns[$header_column]] . "</td>";
}
echo "</tr>";
}
}
echo "</table>";?>


<script language="JavaScript">
function updateDetails(data){
document.getElementById("details").style.visibility = "visible";
for(key in data) {
if(data.hasOwnProperty(key)) {
let value = data[key];
try {
if (key == "last_connection") {
value = new Date(value * 1000.0);
}
if (key == "lang") {
document.getElementById(key).style.background = "url('stats/24/" + value + ".png')";
continue;
}
document.getElementById(key).innerHTML = value;
} catch(e) {
console.log(key)
}

}
}
}
</script>


</body>
</html>
Staubi
Junior Member
Posts: 3
Joined: Sat Dec 31, 2016 1:45 pm

Re: Ranking PHP

Postby Staubi » Sat Dec 31, 2016 1:47 pm

Syntax: Select all

<!DOCTYPE html>
<html>
<head>
<script src="sorttable.js"></script>
<style>
body {
background-color: black;
color: white;
font-family: "Trebuchet MS", sans-serif;
font-size: 16px;
background-repeat: no-repeat;
background-image: url(http://hlsw.rocks-clan.de/~akooh/fastDL/config/background01.png);
background-attachment: fixed;
text-shadow: 0px 1px 2px black;
}

h1 {
padding: none;
margin: none;
font-size: 24px;
}

h1 em {
font-size: 16px;
font-weight: bold;
display: inline-block;
}

table.sortable tr td:first-child {
text-align: center;
}

table.sortable th:not(.sorttable_sorted):not(.sorttable_sorted_reverse):not(.sorttable_nosort):after {
content: " \25B4\25BE";
}

#details {
float: right;
padding: 0;
margin: 0;
}

#details table {
border-spacing: 0;
border-collapse: collapse;
}

.grid {
display: flex;
flex-direction: row;
}

.grid * {
flex-grow: 1;
width: 50%;
}

.legend {
display: block;
float: left;
width: 10em;
font-weight: bold;
}

#details {
width: 25%;
visibility: hidden;
position: fixed;
right: 1em;
}

#details table th {
text-align: left;
}

#details table h2 {
padding: none;
margin: none;
padding-top: 1.5em;
}

#lang {
display: block;
top: 0;
right: 0;
position: absolute;
width: 24px;
height: 24px;
}

.clickable {
cursor: pointer;
}
</style>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>

<div id="details">
<table>
<tr>
<td colspan="2">
<h1 id="name">Fuckin Fucks</h1><i id="lang"><i>
</td>
</tr>
<tr><th class="legend">last connected on:</th> <td id="last_connection">13. Juni 2016 11h23</td></tr>
<tr><th class="legend">Total Points:</th><td id="points">12312312</td></tr>
<tr><th class="legend">Maps played:</th> <td id="maps">71</td></tr>
<tr><th class="legend">Jetpacks left:</th> <td id="jet">2</td></tr>

<tr><td colspan="2"><h2>Kills</h2></td></tr>

<tr><th class="legend">Kills:</th> <td id="kills">2130</td></tr>
<tr><th class="legend">Deaths:</th> <td id="deaths">860</td></tr>
<tr><th class="legend">Suicides:</th> <td id="suicides">29</td></tr>

<tr><td colspan="2"><h2>Records</h2></td></tr>
<tr><th class="legend">Consecutive Kills:</th> <td id="consecutive_kills">45</td></tr>
<tr><th class="legend">Max pts for 1 kill:</th> <td id="max_point">1234</td> </tr>

<tr><th class="legend">Distance:</th> <td id="distance">109</td></tr>
<tr><th class="legend">Postal:</th> <td id="postal">6</td></tr>

<tr><td colspan="2"><h2>Naughty</h2></td></tr>
<tr><th class="legend">Punished:</th> <td id="punished">45</td></tr>
<tr><th class="legend">Kicked:</th> <td id="kicks">1234</td></tr>
<tr><th class="legend">Ban:</th> <td id="reason_for_ban"></td></tr>
</table>
</div>

<h1>
Ranking

<?php
function joinPaths() {
$args = func_get_args();
$paths = array();
foreach ($args as $arg) {
$paths = array_merge($paths, (array)$arg);
}

$paths = array_map(create_function('$p', 'return trim($p, "/");'), $paths);
$paths = array_filter($paths);
return join('/', $paths);
}


$ftp_connection = array(
"username" => "",
"password" => "",
"server" => "gs.rocks-clan.de",
"port" => "",
# this is probably the same
"directory" => "home/akooh/gameserver/postalalt/hl2mp",
);

$local_directory = "stats/";
$max_age = 5;

$file_names = array(
"rank.txt",
);
$info_file = "stats/index.json";


function updateLastDownload($info_file){

$json_data = array(
"last_download" => time(),
);
//echo $json_data;
$json_string = json_encode($json_data);
//echo $json_string;
file_put_contents($info_file, $json_string);
}

if (!file_exists($info_file)) {
//echo "File was not existing";
updateLastDownload($info_file);
}

$json_string = file_get_contents($info_file);
$json_data = json_decode($json_string, true);
$age_difference = (time() - $json_data["last_download"]);
$file_too_old = $age_difference > (60 * $max_age);

if($file_too_old) {
//echo "Downloading";
$connection = ftp_connect($ftp_connection["server"]);
$login_successful = ftp_login($connection,
$ftp_connection["username"],
$ftp_connection["password"]);

if((!$connection) || (!$login_successful)) {
echo "[[Error Connecting.]]";
exit;
}
foreach ($file_names as $file_name) {
$server_file = joinPaths($ftp_connection["directory"], $file_name);
$local_file = joinPaths($local_directory, $file_name);

$last_load = ftp_get($connection, $local_file, $server_file, FTP_BINARY);
//echo "Dowload of file ". $file_name . " was successful:" . $last_load;
}


ftp_close($connection);
updateLastDownload($info_file);
}
else {
//echo "file is current";
}
$age_difference_minutes = round($age_difference / 60.0, 0);
if ($age_difference_minutes == 0) {
echo "<em>Last Update: Right now !</em>";
} elseif ($age_difference_minutes == 1) {
echo "<em>Last Update: 1 minute ago.</em>";
} else {
echo "<em>Last Update: " . $age_difference_minutes . " minutes ago.</em>";
}
?>
</h1>
<?php

$columns = array(
"steamid" => 0,
"name" => 1,
"kills" => 2,
"deaths" => 3,
"teamkill" => 4,
"kicks" => 5,
"suicides" => 6,
"points" => 7,
"maps" => 8,
"burn" => 9,
"max_point" => 13,
"punished" => 15,
"reason_for_ban" => 21,
"ban" => 22,
"distance" => 16,
"postal" => 29,
"sensei" => 32,
"headshots" => 25,
"lang" => 18,
"consecutive_kills" => 12,
"last_connection" => 26,
"jet" => 17,
);

foreach($file_names as $file_name) {
$local_file = joinPaths($local_directory, $file_name);
$contents = file_get_contents($local_file);
$file = file($local_file);

function breakit($csv_string) {
return str_getcsv($csv_string, ";");
}

$data = array_map("breakit", $file, array(";"));

function compare($a, $b) {
global $columns;
$lhs = $a[$columns["points"]];
$rhs = $b[$columns["points"]];
if ($lhs == $rhs) return 0;
return $lhs > $rhs ? -1 : 1;
}

uasort($data, compare);
$rank = 1;
$header_columns = array(
"name", "points", "kills",
"deaths", "postal", "sensei", "consecutive_kills",
"suicides", "kicks",
);

$detail_columns = array(
"name",
"last_connection",
"points",
"maps",
"jet",
"kills",
"kicks",
"lang",
"deaths",
"suicides",
"consecutive_kills",
"max_point",
"distance",
"postal",
"punished",
"max_point",
"reason_for_ban"
);
echo "<table class='sortable'>
";


echo "
<tr>
";

echo "
<th>Rank</th>";
foreach($header_columns as $header) {
echo "
<th>" . ucfirst($header) . "</th>";
}
echo "
</tr>";
foreach ($data as $userinfo) {
$javascript_thingi = "updateDetails({";
foreach($detail_columns as $detail ) {
$javascript_thingi .= "'$detail': '";
$javascript_thingi .= $userinfo[$columns[$detail]];
$javascript_thingi .= "',";
}
$javascript_thingi .= "});";
echo "
<tr class=\"clickable\" onclick=\"$javascript_thingi\">
";

echo "
<td>$rank</td>";
$rank++;
foreach ($header_columns as $header_column) {
echo "
<td>" . $userinfo[$columns[$header_column]] . "</td>";
}
echo "
</tr>";
}
}
echo "
</table>";?>


<script language="JavaScript">
function updateDetails(data){
document.getElementById("details").style.visibility = "visible";
for(key in data) {
if(data.hasOwnProperty(key)) {
let value = data[key];
try {
if (key == "last_connection") {
value = new Date(value * 1000.0);
}
if (key == "lang") {
document.getElementById(key).style.background = "url('stats/24/" + value + ".png')";
continue;
}
document.getElementById(key).innerHTML = value;
} catch(e) {
console.log(key)
}

}
}
}
</script>


</body>
</html>
Last edited by Staubi on Sat Dec 31, 2016 1:57 pm, edited 1 time in total.
User avatar
Painkiller
Senior Member
Posts: 725
Joined: Sun Mar 01, 2015 8:09 am
Location: Germany
Contact:

Re: Ranking PHP

Postby Painkiller » Sat Dec 31, 2016 7:16 pm

Does not work yet

Sorttable.js, index.json are created
Staubi
Junior Member
Posts: 3
Joined: Sat Dec 31, 2016 1:45 pm

Re: Ranking PHP

Postby Staubi » Sun Jan 01, 2017 2:14 pm

So würde erst mal dieses Script ausprobieren, um zu sehen ob überhaupt Daten geholt werden können.
Wenn es funktioniert kann man sich Gedanken über die Formatierung machen.

Tausche für die Verwendung einfach folgende Strings mit den richtigen Daten aus:
TABELLENNAME
localhost (bleibt meistens gleich)
BENUTZER (Datenbankbenutzer)
PASSWORT (Datenbankbenutzerpasswort)
DATENBANKNAME

Syntax: Select all

<?php
 
 
 /** Tabellenname definieren**/
 define(TABLENAME, "TABELLENNAME");
 
 /** Datenbankverbindung definieren**/
 $con = mysql_connect("localhost","BENUTZER","PASSWORT");
 if (!$con)die('Keine Verbindung zur Datenbank' . mysql_error());
 
 mysql_select_db("DATENBANKNAME") or die("Datenbank nicht vorhanden");

 echo "<table border='1'><tr>";
 
 $sortBy = (isset($_GET["sortBy"])) ? $_GET["sortBy"] : mysql_field_name(mysql_query("SELECT * FROM " . TABLENAME), 0);
 $direction = (isset($_GET["direction"])) ? $_GET["direction"] : "ASC";
 
 $sql = "SELECT * FROM " . TABLENAME .  " ORDER BY " . $sortBy . " " . $direction;
 $query = mysql_query($sql);
 $feldcontainer = array();
 
 /** Spaltenüberschriften **/
 echo "<tr>";
 
 for($i = 0; @$feld = mysql_field_name($query, $i); $i++):

 echo "<td align='center'><strong>" . $feld . " <a href='?sortBy=$feld&direction=ASC'>&uarr;</a> &nbsp; <a href='?sortBy=$feld&direction=DESC'>&darr;</a>" . "</strong></td>";
 
 $feldcontainer[$i] = $feld;
 
 endfor;
 
 echo "</tr>";
 
 /** Zeilen **/
 
if(mysql_num_rows($query)):
 
 while($row = mysql_fetch_object($query)):
 
 echo "<tr>";
 
 foreach($feldcontainer AS $key => $value):
 
 /** $key und nächstes if werden nur für die Spaltenfarbe benötigt **/
 
 if($key % 2 == null): $col = "#EEFDC1"; else: $col = "#EEEEFF"; endif;
 
 echo "<td bgcolor='$col'>" . $row->$value . "</td>";
 
 endforeach;
 
 echo "</tr>";
 
 endwhile;

else: echo "<tr><td colspan='" . count($feldcontainer) ."'>Es befinden sich keine Eintr&auml;ge in der Datenbank</td></tr>";
 
endif;
?>
User avatar
Kill
Member
Posts: 88
Joined: Wed Aug 31, 2016 10:05 pm

Re: Ranking PHP

Postby Kill » Sun Jan 01, 2017 8:02 pm

Staubi wrote:So würde erst mal dieses Script ausprobieren, um zu sehen ob überhaupt Daten geholt werden können.
Wenn es funktioniert kann man sich Gedanken über die Formatierung machen.

Tausche für die Verwendung einfach folgende Strings mit den richtigen Daten aus:
TABELLENNAME
localhost (bleibt meistens gleich)
BENUTZER (Datenbankbenutzer)
PASSWORT (Datenbankbenutzerpasswort)
DATENBANKNAME

Syntax: Select all

<?php
 
 
 /** Tabellenname definieren**/
 define(TABLENAME, "TABELLENNAME");
 
 /** Datenbankverbindung definieren**/
 $con = mysql_connect("localhost","BENUTZER","PASSWORT");
 if (!$con)die('Keine Verbindung zur Datenbank' . mysql_error());
 
 mysql_select_db("DATENBANKNAME") or die("Datenbank nicht vorhanden");

 echo "<table border='1'><tr>";
 
 $sortBy = (isset($_GET["sortBy"])) ? $_GET["sortBy"] : mysql_field_name(mysql_query("SELECT * FROM " . TABLENAME), 0);
 $direction = (isset($_GET["direction"])) ? $_GET["direction"] : "ASC";
 
 $sql = "SELECT * FROM " . TABLENAME .  " ORDER BY " . $sortBy . " " . $direction;
 $query = mysql_query($sql);
 $feldcontainer = array();
 
 /** Spaltenüberschriften **/
 echo "<tr>";
 
 for($i = 0; @$feld = mysql_field_name($query, $i); $i++):

 echo "<td align='center'><strong>" . $feld . " <a href='?sortBy=$feld&direction=ASC'>&uarr;</a> &nbsp; <a href='?sortBy=$feld&direction=DESC'>&darr;</a>" . "</strong></td>";
 
 $feldcontainer[$i] = $feld;
 
 endfor;
 
 echo "</tr>";
 
 /** Zeilen **/
 
if(mysql_num_rows($query)):
 
 while($row = mysql_fetch_object($query)):
 
 echo "<tr>";
 
 foreach($feldcontainer AS $key => $value):
 
 /** $key und nächstes if werden nur für die Spaltenfarbe benötigt **/
 
 if($key % 2 == null): $col = "#EEFDC1"; else: $col = "#EEEEFF"; endif;
 
 echo "<td bgcolor='$col'>" . $row->$value . "</td>";
 
 endforeach;
 
 echo "</tr>";
 
 endwhile;

else: echo "<tr><td colspan='" . count($feldcontainer) ."'>Es befinden sich keine Eintr&auml;ge in der Datenbank</td></tr>";
 
endif;
?>



Very bad. There's db injections everywhere... Use mysqli or pdo and prepared statements.
User avatar
Painkiller
Senior Member
Posts: 725
Joined: Sun Mar 01, 2015 8:09 am
Location: Germany
Contact:

Re: Ranking PHP

Postby Painkiller » Wed Jan 04, 2017 4:55 pm

Ok the mysql database is he makes logins are in it.

Still does not work itself Error on the page

http://hlsw.rocks-clan.de/~akooh/fastDL ... _stats.php
Staubi
Junior Member
Posts: 3
Joined: Sat Dec 31, 2016 1:45 pm

Re: Ranking PHP

Postby Staubi » Wed Jan 04, 2017 10:03 pm

I told you before, this Code is only for testing the MYSQl Login.
When the Login is still working, you can take the Code from my first post, Change the Login with the wright datas.
Greets
User avatar
Painkiller
Senior Member
Posts: 725
Joined: Sun Mar 01, 2015 8:09 am
Location: Germany
Contact:

Re: Ranking PHP

Postby Painkiller » Mon Jan 09, 2017 3:19 pm

I would still be glad about help

Return to “Whatever”

Who is online

Users browsing this forum: No registered users and 16 guests