[PHP/MySQL]Live Score Board

Hello all,
First let me say thanks to all that help on these boards, as i have gotten alot of help here.

Second
i am building a live score board for a gaming server to display who is in my gaming server and it works great as is … but i am trying to format it better to group the teams together and then group/sort by squad.

My Apologies if i have placed this in the wrong forum, i wasnt sure if it should go into the php section or here.

here is what i have currently…
[php]

<?php $con=mysqli_connect("host","user","password","table"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $query = 'SELECT Soldiername, Kills, Deaths, TeamID, SquadID FROM tbl_currentplayers'; $result = mysqli_query($con,$query); $num_rows = $result->num_rows; if($num_rows > 0) { echo ""; while($row = mysqli_fetch_assoc($result)) { IF ($row['TeamID'] == "1"){ $team = "US" ; }else{ $team = "RU" ; } IF ($row['SquadID'] == "1"){ $squad = "Alpha"; }elseif ($row['SquadID'] == "2"){ $squad = "Bravo"; }elseif ($row['SquadID'] == "3"){ $squad = "Charlie"; }elseif ($row['SquadID'] == "4"){ $squad = "Delta"; }elseif ($row['SquadID'] == "5"){ $squad = "Echo"; }elseif ($row['SquadID'] == "6"){ $squad = "Foxtrot"; }elseif ($row['SquadID'] == "7"){ $squad = "Golf"; }elseif ($row['SquadID'] == "8"){ $squad = "Hotel"; }elseif ($row['SquadID'] == "9"){ $squad = "Inda"; }elseif ($row['SquadID'] == "10"){ $squad = "Juliet"; }elseif ($row['SquadID'] == "11"){ $squad = "Kilo"; }elseif ($row['SquadID'] == "12"){ $squad = "Lima"; }elseif ($row['SquadID'] == "13"){ $squad = "Mike"; }elseif ($row['SquadID'] == "14"){ $squad = "November"; }elseif ($row['SquadID'] == "15"){ $squad = "Oscar"; }elseif ($row['SquadID'] == "16"){ $squad = "Papa"; }elseif ($row['SquadID'] == "17"){ $squad = "Quebec"; }elseif ($row['SquadID'] == "18"){ $squad = "Romeo"; }elseif ($row['SquadID'] == "19"){ $squad = "Siera"; }elseif ($row['SquadID'] == "20"){ $squad = "Tango"; }elseif ($row['SquadID'] == "21"){ $squad = "Uniform"; }elseif ($row['SquadID'] == "22"){ $squad = "Victor"; }elseif ($row['SquadID'] == "23"){ $squad = "Whiskey"; }elseif ($row['SquadID'] == "24"){ $squad = "Xray"; }elseif ($row['SquadID'] == "25"){ $squad = "Yankee"; }elseif ($row['SquadID'] == "26"){ $squad = "Zulu"; }elseif ($row['SquadID'] == "0"){ $squad = "Not In Squad"; } echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } }else { echo "
Player Name Squad K/D
" . $row['Soldiername'] . "" . $team . "" . $squad . "" . $row['Kills'] . "/" . $row['Deaths'] . "
"; echo "" ; } echo "" ; echo "
No Players Online
Play Now!
"; mysqli_close($con); ?>

[/php]

so once its formatted correctly it would show this… but im not too sure how to do this and weather its better to do i with the mysql statement using group by and order by or what … and as long as the output is similar then im not too picky on the table structure. Other than i want the Team Names as headers due to formatting of cells and such.

[code]

john Alpha 10/2bob Alpha 5/13tim charlie 2/10james echo 3/5
US Team
pete Alpha 5/2steve bravo 8/20eric charlie 6/8chico charlie 4/5
RU Team
[/code]

What does your table design look like?

A quick example on how I would go about it:

[php]SELECT
t.teamName
,p.playerName
FROM
player p
INNER join
team_player tp
ON
p.playerID = tp.playerID
LEFT JOIN
team t
ON
tp.teamID = t.teamID
ORDER BY
teamName
[/php]

Also, what is all the silly IF’s like: }elseif ($row[‘SquadID’] == “3”){ $squad = “Charlie”;
???
You should either place all of them into an array and then just display the results or even better save them in
a table with squadID’s and squad names and just pull them out as needed… All of those nested IF’s are just not
needed if you think out the data better…

Table Structure

CREATE TABLE `tbl_currentplayers` ( `ServerID` smallint(6) NOT NULL, `Soldiername` varchar(45) NOT NULL, `GlobalRank` smallint(5) unsigned NOT NULL DEFAULT '0', `ClanTag` varchar(45) DEFAULT NULL, `Score` int(11) NOT NULL DEFAULT '0', `Kills` int(11) NOT NULL DEFAULT '0', `Headshots` int(11) NOT NULL DEFAULT '0', `Deaths` int(11) NOT NULL DEFAULT '0', `Suicide` int(11) DEFAULT NULL, `Killstreak` smallint(6) DEFAULT '0', `Deathstreak` smallint(6) DEFAULT '0', `TeamID` tinyint(4) DEFAULT NULL, `SquadID` tinyint(4) DEFAULT NULL, `EA_GUID` varchar(45) NOT NULL DEFAULT '', `PB_GUID` varchar(45) NOT NULL DEFAULT '', `IP_aton` int(11) unsigned DEFAULT NULL, `CountryCode` varchar(2) DEFAULT '', `Ping` smallint(6) DEFAULT NULL, `PlayerJoined` datetime DEFAULT NULL, PRIMARY KEY (`ServerID`,`Soldiername`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

As for the IF/ElseIFs … i plan on useing them in an array just getting it working first then will make code pretty

As [member=43746]ErnieAlex[/member] said, you should have a table with the id_num/names (BEST PRACTICE). But… As is you could use the Mysql CASE function although I don’t recommend it in your case.

[php]SELECT
Soldiername,
Kills,
Deaths,
TeamID,

CASE SquadID
WHEN 1 THEN ‘Alpha’
WHEN 2 THEN ‘Bravo’
WHEN 3 THEN ‘Charlie’
WHEN 4 THEN ‘Delta’
END

FROM
tbl_currentplayers[/php]

i appreciate the pointers on how to handle the if/esle statements however my original question still hasnt been resolved.

Well, actually they did answer it. You can alter your query to join the data in the way you want to. The samples
were just basic ideas on how you would do this. You could also just group and order the data in the query also.

Here are some links that explain grouping and ordering… Should help…

http://www.w3schools.com/sql/sql_groupby.asp
http://www.w3schools.com/sql/sql_orderby.asp
http://stackoverflow.com/questions/1066453/mysql-group-by-and-order-by

Firstly thanks for the links they helped a little…
so i kinda understand the grouping, and have already pulled the data, i’m looking for a way to display it now so that i basically have 2 sections on output … I would like to do this all within the sql but im not sure if i can or not. Within those 2 sections display the players inside the squad sections only having squads that are in use show up… so if i have 6 players in the server

3 on each team

for team 1(US)
2 players are on alpha
1 player is on charlie

for team 2 (RU)
2 players on alpha
1 player on bravo squad

the output would be … (i’m using a bullet list here just as an example the end result will be in a table)
Notice there is no Bravo in US team listing because there are no players in that squad

[ul][li]US:[/li]
[list]
[li]Alpha Squad[/li]
[list]
[li] Player3[/li]
[li]Player6[/li]
[/list][/list]
[list]
[li]Charlie Squad[/li]
[list]
[li]Player2[/li]
[/list][/list][/ul]

[ul][li]RU:[/li]
[list]
[li]Alpha Squad[/li]
[list]
[li]Player1[/li]
[li]Player4[/li]
[/list][/list]
[list]
[li]Bravo Squad[/li]
[list]
[li]Player5[/li]
[/list][/list][/ul]

on a side note … do i really have to do a join since all the data is in 1 table?
the data in that table is only there when someone is in the server once they leave they are also removed from the table.

sorry for teh double post but i couldnt edit the previous…

UPDATE:
i have created 2 new tables

tbl_squads
squadID
squadName

tbl_teams
teamID
teamName

now the IF/Else statements are gone
and have changed the original code to:

[php]<?php
$con=mysqli_connect(“host”,“user”,“pass”,“db”);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$query = ‘SELECT
a.Soldiername,
a.Kills,
a.Deaths,
a.CountryCode,
c.squadName,
b.teamName
FROM
tbl_currentplayers AS a
LEFT JOIN tbl_teams AS b ON b.teamID = a.TeamID
LEFT JOIN tbl_squads AS c ON c.squadID = a.SquadID
ORDER BY
b.teamID ASC,
c.squadName ASC’;

$result = mysqli_query($con,$query);
$num_rows = $result->num_rows;
if($num_rows > 0) {
echo “<table class=“onlinePlayers”>

Player Name Team Squad K/D ”;
while($row = mysqli_fetch_assoc($result))
{
echo “”;
echo “” . $row[‘a.Soldiername’] . “”;
echo “” . $row[‘b.teamname’] . “”;
echo “” . $row[‘c.squadname’] . “”;
echo “” . $row[‘a.Kills’] . “/” . $row[‘Deaths’] . “”;
echo “”;
}
}else {
echo “<table class=“onlinePlayers”>”;
echo “<td colspan=“4”>No Players Online” ;
}
echo “<td colspan=“4”><a class=“button” href=”#" target="_blank">Play Now!" ;
echo “”;
mysqli_close($con);
?>[/php]

Is it working for you?

not the way i want … but it is displaying the results its just a formatting issue at this point and im not sure how to get it to format the way i want. you can see the DEMO

[php]<?php
$con=mysqli_connect(“host”,“user”,“pass”,“db”);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$query = ‘SELECT
cp.Soldiername,
cp.Kills,
cp.Deaths,
cp.Score,
s.squadName,
t.teamName
FROM
tbl_currentplayers AS cp
LEFT JOIN tbl_teams AS t ON t.teamID = cp.TeamID
LEFT JOIN tbl_squads AS s ON s.squadID = cp.SquadID
ORDER BY
t.teamID ASC,
s.squadName ASC’;

$result = mysqli_query($con,$query);
$num_rows = $result->num_rows;
if($num_rows > 0) {
echo “<table class=“onlinePlayers”>

Player Name Team Squad K/D Stats ”;
while($row = mysqli_fetch_assoc($result)){
echo “”;
echo “” . $row[‘Soldiername’] . “”;
echo “” . $row[‘teamName’] . “”;
echo “” . $row[‘squadName’] . “”;
echo “” . $row[‘Kills’] . “/” . $row[‘Deaths’] . “”;
echo “<a href=“https://i-stats.net/index.php?action=pcheck&player=” . $row[‘Soldiername’] . “&game=BF3&sub=Check+Player” target=_blank>iStats | <a href=“http://metabans.com/search/” . $row[‘Soldiername’] . “” target=_blank>MetaBans”;
echo “”;
}
}else {
echo “<table class=“onlinePlayers”>”;
echo “<td colspan=“5”>No Players Online” ;
}
echo “<td colspan=“5”><a class=“button” href=“http://battlelog.battlefield.com/bf3/servers/show/pc/8c863f97-1369-4c4b-bf17-c369bd2adf88/Kryptic-Killers-24-7-Metro-1000tix-PBBans-PBScreens/” target=”_blank">Play Now!" ;
echo “”;
mysqli_close($con);
?> [/php]

Well, the results of a query is simply an array of data. If you have the array in hand (after a query) and you
know there is data inside it, you can simple loop thru the data and make sure it breaks as needed. Just keep
a pointer to the current team and current squad. When they change, display the title for the next one. Only
display the team name and squad name once for each change… Loosely, something like:

    Teams: $current_team="xyz"; $current_squad="xyz"; while(loop thru the results...) { If $row[$team]!=$current_team { $current_team=$row[$team]; // set current team to new-current team echo "
  • " . $row["team"] . "
    • "; // close previous UL and start a new one... } If $row[$squad]!=$current_squad { $current_squad=$row[$squad]; // set current squad to new-squad team echo "
    • " . $row["squad"] . "
    • "; } }

    This is just a loose explanation, not tested code… You need to just know when to break the UL’s. Since you
    want them broken on teams, the above will work. The titles need to be the first ones after the new UL’s and
    you have to nest the UL’s for the squad members. Hope that makes sense. Once you get further, show us
    what you end up with. If you can’t get it to work, we can help further…

    i guess i am getting way ahead of my abilities, cause i am even more confused now. I realize that you are trying to help by giving the “idea” to use and letting me learn by doing but for some reason i am still lost. i really wish i could show you an example of what i am wanting to do so that you could help me understand better. … or perhaps i am over complicating all of this?? lets do this … what would the best practice way be to display the data that i want to display in an easy to read organized manner that fits in the area that it currently is in?

    draw.io

    I think in order for us to help, you need to know and be able to communicate what you want. Pictures work. The link above is what we use for diagraming layouts.

    Jay, you mentioned you have only one table and then you made two tables. We should start there. How many
    teams do you have? If only the two that you showed, then there is no need for two tables. If there are many
    such as hundreds, then you would do better with two tables. In which case, use the join to get the names from
    the indexed table of squads. Either way, you end up with a table of data that contains the team name, squad
    name and members of the squads. Loosely in the form of TEAM, SQUAD, SOLDIERS and that basically means
    that you just need to loop thru the results as I showed you and break where needed. There are many other
    ways to handle this. I found a nice recursive version, but, it keeps running queries for the members. The way
    I showed you should work. Here is a version combined with the code you posted. Without access to the DB,
    not sure if it will work, but, test it and post the results…

    First, think of the bullet code. Here is how it looks for the display using my own samples:

      team name 1
        squad name 1
      • Soldiername1
      • Soldiername2
        squad name 2
      • Soldiername3
      • Soldiername4
      team name 2
        squad name 3
      • Soldiername5
      • Soldiername6
    So, as you see, there are only two break points. First is teams and next squads. This makes sense, right? Your code would have to check for each and display it as needed. Using the correct
      ,
    • ,
    • and
    tags where they are needed. Here is a sample that might work for you. It is an improved version of the sample I posted earlier. Try it and let us know if it works or not for you... [php] <?php $result = mysqli_query($con,$query); $num_rows = $result->num_rows; if($num_rows > 0) { $current_team="xyz"; $current_squad="xyz"; while(loop thru the results...) { If($row[$team]!=$current_team) { if($current_team!="xyz") echo ""; // close the previous team group $current_team=$row[$team]; // set current team to new-current team echo "
      " . $row["team"]; // show team as top level... } If $row[$squad]!=$current_squad { if($current_squad!="xyz") echo "
    "; // close the previous squad group $current_squad=$row[$squad]; // set current squad to new-squad team echo "
      " . $row["squad"]; // show squad as sub-level... } echo "
    • " . $row["Soldiername"] . "
    • "; // show Soldiername as lowest-level... } echo "
    "; // close the entire group... } else { echo "No players online at this time!
    "; } ?> [/php] Note that the "xyz" is just a name of a team that does not exist. It is used to start the loop with to know where the code should end and display the correct ending of a group of data. This makes the end ( ) tag work. The above is not tested as no DB connection. If it does not work for you, I can create a test array to use, but it should work. It should create the needed bullet list for you. Let us know how it works for you...

    here is the diagram that i am trying to achieve…

    http://prntscr.com/a4zuyn

    The database is populated by a 3rd party program running on the game server that i cannot edit, which means that i cannot populate the 2 tables that i made with any of the data from the game server. so i am limited in the data that i can use. That is why i made 2 tables one with the correlation between the teamID and teamName and the other for the correlation between the squadID and squadName

    If you can read the data from their database, then you can create your own version of the data. But, if you can
    read their data and just want to display it, you do not need to. In that case, just use their data…

    Your diagram shows a table, not a bullet list. To create this table, it would be very simple and the easiest way is
    to just do two queries and create two nested tables. This is simple enough to do. Similar to the bullet list only
    two queries, one for each team and just a straightforward table display with headings and data cells. Is that what
    you need help with now?

    yes that has been the request from the get-go … ive never wanted to do bullet list i even stated in my replies that i wanted a table and that i was only useing the bullet list to help describe what i was looking for as an example. however i do appreciate all the suggestions as it helps me learn new and better ways of doing things, so in the future if i ever need to do a dynamic bullet list i now know how to.

    This is actually where those really irritating drawing with asterisks comes in handy, as that is kind of what you are doing.

    As Ernie said, it is just a matter of knowing when to change the tags in a loop.

    Jay, To create a table instead of bullet lists, you just have to lay out the structure first. Anyway, the layout you
    posted shows the layout. But, you should do this in HTML with just text and get it to look like you want it to be.
    If you need table help, post us your questions. Just get it to look like you want it and then, we can help you get
    the data into it. The basic way I showed you for bullet’s should work with a few changes. Some of the

      's
      will become 's and some will become 's. Not too complicated. Well, give it a try and we can help when
      you get stuck. And, sorry that I was confused on what you wanted… I was just following your example…
    Sponsor our Newsletter | Privacy Policy | Terms of Service