Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- In this tutorial, we're going to learn how to make a leaderboard for KillAnnouncer!
- Let's get started. It is recommended that you know basic PHP and HTML/CSS in order to follow
- this tutorial. A source download will be avaialable at the end of this tutorial.
- After setting up KillAnnouncer, you'll see that it automatically fills a table with 4 rows. These
- rows include "PlayerName", "Kills","Deaths", and "Ratio".
- We're going to need that information laters so keep it handy somewhere.
- _-Connecting to the database-_
- Connecting to a mysql database via php is easy, simple open the connection.
- [code PHP]
- <?php
- $hostname = ''; //Your mysql hostname, usually localhost
- $username = ''; //your mysql username
- $password = ''; //your mysql password
- $database = ''; //your mysql database name
- $con = mysql_connect($hostname, $username, $password);
- if (!$con){ //If the connection failed...
- die('Could not connect! Error:'.mysql_error()); //display error
- } //end if statement
- ?>
- [/code PHP]
- That will allow you to connect to the database. Now if you're going to be using mysql throughout
- your website, I suggest you save this in another file named db.connect.inc.php. If you're not
- going to be using mysql alot and you just need it for the leaderboard, keep it in leaderboard.php
- Now go ahead and start making the table for your leaderboard.
- [code PHP]
- <table class="table table-striped">
- <thead>
- <tr>
- <th>K/D Ratio</th>
- <th>Username</th>
- <th>Kills</th>
- <th>Deaths</th>
- </tr>
- </thead>
- <tbody>
- </tbody>
- </table>
- [/code PHP]
- Now that we've got our table all settled, let's go ahead and add some php to get the info from the
- datbase and display it.
- [code PHP]
- <tbody>
- <?php
- $sql ="SELECT * FROM leaderboard ORDER BY 'Ration'";
- $res = mysql_query($sql);
- while ($row = mysql_fetch_array($res)) {
- echo "<tr>";
- echo "<td>".$row['Ratio']."</td>";
- echo "<td>".$row['PlayerName']."</td>";
- echo "<td>".$row['Kills']."</td>";
- echo "<td>".$row['Deaths']."</td>";
- echo "</tr>";
- }
- ?>
- </tbody>
- [/code PHP]
- The code above spits out those rows for each row in the datbase, It's a loop that will display all
- the enterys in the database.
- This is what your whole page should look like...
- [code PHP]
- <?php
- $hostname = ''; //Your mysql hostname, usually localhost
- $username = ''; //your mysql username
- $password = ''; //your mysql password
- $database = ''; //your mysql database name
- $con = mysql_connect($hostname, $username, $password);
- if (!$con){ //If the connection failed...
- die('Could not connect! Error:'.mysql_error()); //display error
- } //end if statement
- ?>
- <html>
- <head>
- <title>KillAnnouncer Leaderboard</title>
- </head>
- <body>
- <table class="table table-striped">
- <thead>
- <tr>
- <th>K/D Ratio</th>
- <th>Username</th>
- <th>Kills</th>
- <th>Deaths</th>
- </tr>
- </thead>
- <tbody>
- <?php
- $sql ="SELECT * FROM leaderboard ORDER BY 'Ration'";
- $res = mysql_query($sql);
- while ($row = mysql_fetch_array($res)) {
- echo "<tr>";
- echo "<td>".$row['Ratio']."</td>";
- echo "<td>".$row['PlayerName']."</td>";
- echo "<td>".$row['Kills']."</td>";
- echo "<td>".$row['Deaths']."</td>";
- echo "</tr>";
- }
- ?>
- </tbody>
- </table>
- </body>
- </html>
- [/code PHP]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement