First of all, im a beginner. And the script is probably coded bad.
Feel free to show me a way to optimize.
What does the code?
- Highscores sort by experience
- Name and level
- Exp/Day
- And the ranking (how many position up or down)
Example:
Pos: 5
Name: alka
Level: 50
Exp/day: 1244000
+-: 5
How the does it work?
Just run in it once a day.
Here is the code:
1. Create a table in your database
2. Create a php file with this content and exucute.
3. You will get HTML file with the content.
Just add in your homepage and you are done.
And once again, dont blame me. Im totaly beginner.
Feel free to show me a way to optimize.
What does the code?
- Highscores sort by experience
- Name and level
- Exp/Day
- And the ranking (how many position up or down)
Example:
Pos: 5
Name: alka
Level: 50
Exp/day: 1244000
+-: 5
How the does it work?
Just run in it once a day.
Here is the code:
1. Create a table in your database
Code:
CREATE TABLE IF NOT EXISTS `player_stats` (
`nummer` int(10) NOT NULL,
`id` int(11) NOT NULL,
`name` varchar(255) character set latin1 collate latin1_general_ci NOT NULL,
`level` int(11) NOT NULL default '1',
`vocation` int(11) NOT NULL,
`current_experience` bigint(20) NOT NULL default '0',
`new_experience` bigint(20) NOT NULL,
`old_experience` bigint(20) NOT NULL,
`experience_diffrence` bigint(20) NOT NULL,
`old_nummer` int(2) NOT NULL,
`nummer_diff` int(2) NOT NULL,
`chara_is_new` varchar(3) character set latin1 collate latin1_general_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
2. Create a php file with this content and exucute.
Code:
<?php
mysql_connect("localhost","username","password") or die(mysql_error());
mysql_select_db("name_of_the_database") or die(mysql_error());
$abfrage = "SELECT id, name, level, vocation, experience FROM players ORDER BY experience DESC LIMIT 100";
$ergebnis = mysql_query($abfrage);
$id = array();
$name = array();
$level = array();
$vocation = array();
$exp = array();
while($ansprechbar = mysql_fetch_array($ergebnis))
{
array_push($id, $ansprechbar[id]);
array_push($name, $ansprechbar[name]);
array_push($level, $ansprechbar[level]);
array_push($vocation, $ansprechbar[vocation]);
array_push($exp, $ansprechbar[experience]);
}
$abfrage2 = "SELECT id, name, new_experience FROM player_stats";
$ergebnis2 = mysql_query($abfrage2);
$da_exp = array();
$id2 = array();
$name2 = array();
$neu = array();
$k = 0;
$a = 1;
foreach ($id as $key => $id3)
{
$abfrage3 = "SELECT nummer, id, new_experience FROM player_stats WHERE id = '".$id3."'";
$ergebnis3 = mysql_query($abfrage3);
$ansprechbar2 = mysql_fetch_array($ergebnis3);
array_push($id2, $ansprechbar2[id]);
array_push($da_exp, $ansprechbar2[new_experience]);
if($id3 == $ansprechbar2[id])
{
$die_exp = $ansprechbar2[new_experience];
$place = $ansprechbar2[nummer];
$eintrag = "UPDATE player_stats Set old_experience = '".$die_exp."', old_nummer = '".$place."' WHERE id = '".$id3."'";
$eintragen = mysql_query($eintrag);
echo "$a ok. Playerid: $ansprechbar2[id]<br> ";
$k++;
$a++;
}
else
{
echo "$name[$key] ist neu in der highscore liste. Er ist auf Platz $a. <br>";
array_push($neu, $id3);
$a++;
$k++;
}
}
// part 2 here now
$i = 0;
$p = 0;
$t = 1;
$eintrags = "UPDATE player_stats Set chara_is_new = 'yes'";
$eintragens = mysql_query($eintrags);
foreach ($id as $key2 => $wert)
{
$abfrage4 = "SELECT old_nummer, nummer, id, new_experience FROM player_stats WHERE id = '".$wert."'";
$ergebnis4 = mysql_query($abfrage4);
$ansprechbar3 = mysql_fetch_array($ergebnis4);
if($wert == $ansprechbar3[id])
{
$new_place = $ansprechbar3[old_nummer] - $t;
$exp_diff = $exp[$key2] - $da_exp[$key2];
$eintrag2 = "UPDATE player_stats Set chara_is_new = 'no', name = '$name[$i]', id = '$id[$i]', level = '$level[$i]', vocation = '$vocation[$i]', current_experience = '$exp[$i]', new_experience = '$exp[$i]', experience_diffrence = '$exp_diff', nummer = '$t', nummer_diff = '$new_place' WHERE id = '".$wert."'";
// $eintrag2 = "INSERT INTO player_stats (nummer, id, name, level, vocation, current_experience, new_experience) VALUES ('$key2', '$id[$key2]', '$name[$key2]', '$level[$key2]', '$vocation[$key2]', '$exp[$key2]', '$exp[$key2]')";
$eintragen2 = mysql_query($eintrag2);
$i++;
$t++;
}
else
{
// $eintrag3 = "UPDATE player_stats Set id = '$id[$i]', name = '$name[$i]', level = '$level[$i]', vocation = '$vocation[$i]', current_experience = '$exp[$i]', new_experience = '$exp[$i]', experience_diffrence = '$exp_diff' WHERE nummer = '".$neu2."'";
$eintrag3 = "INSERT INTO player_stats (chara_is_new, nummer, id, name, level, vocation, current_experience, new_experience, experience_diffrence, nummer_diff) VALUES ('new', '$t', '$id[$i]', '$name[$i]', '$level[$i]', '$vocation[$i]', '$exp[$i]', '$exp[$i]', '0', '$new_place')";
$eintragen3 = mysql_query($eintrag3);
$i++;
$p++;
$t++;
}
echo "$exp_diff exp. Highscorerank $new_place. <br>";
}
$loschen = "DELETE FROM player_stats WHERE chara_is_new = 'yes'";
$loschen_ex = mysql_query($loschen);
echo "ja ich laufe $i mal durch und p ist gleich $p";
// Ausgabe
$abfrage10 = "SELECT nummer, name, level, new_experience, experience_diffrence, nummer_diff FROM player_stats ORDER BY new_experience DESC";
$ergebnis10 = mysql_query($abfrage10);
$cachefile = "top100.html";
$b = '<div style="float:left;width:50px">Pos</div>
<div style="float:left;width:160px">Name</div>
<div style="float:left;width:100px">Level</div>
<div style="float:left;width:160px">Exp/day</div>
<div style="float:left;width:50px">+-</div>
<br>';
$fp2 = fopen($cachefile, 'w');
fwrite($fp2, $b);
while($row = mysql_fetch_array($ergebnis10))
{
ob_start();
echo '
<div style="float:left;width:50px">'.$row['nummer'].'</div>
<div style="float:left;width:160px"><a href="index.php?seite=search&char='.$row['name'].'">'.$row['name'].'</a></div>
<div style="float:left;width:100px">'.$row['level'].'</div>
<div style="float:left;width:160px">'.$row['experience_diffrence'].'</div>
<div style="float:left;width:50px">'.$row['nummer_diff'].'</div>
<br>';
fwrite($fp2, ob_get_contents());
}
fclose($fp2);
ob_end_flush();
?>
3. You will get HTML file with the content.
Just add in your homepage and you are done.
And once again, dont blame me. Im totaly beginner.