Jump to content

[SOLVED] Split and explode help


slapdashgrim

Recommended Posts

okay i have been working on a script that keeps track of how many players, maps are in my game server and more information about it.

u can view it here http://grimhq.com/serverstatus2.php

i have it all working right now except one thing. in the game there are 2 teams a red and a blue, i want to make it so when it lists the names of the players in the server (witch it does) it will make the name red or blue depending on the team.

i have a mysql database that updates every 30 sec with info on the server. it outputs the names like this

Lit._The Fist_GRIM-TL-BB11_GRIM-KP1-PD_CHARLy-bR0WN_GRIM-S-M0NK3Y_GRIM-KP1-Dakilr_GRIM-M-LockOut_landsurveyor

and the teams like this

1_2_1_2_2_1_1_1_2

the numbers corresponds to the name meaning the team (1 is blue and 2 is red)

 

i want to make it so when it prints the names of the players if makes them red or blue depending on the team.

 

i have been told i need to use split or explode to split up the team numbers but i still am confused.

 

 

any help is appreciated.

 

 

Link to comment
Share on other sites

Okay i have worked further on this and found out a bit about explode

I want to use some thing like

<?php
$teams2= '2_1_2_2_1_1_2'
$teams=explode("_", $teams2);
$count=count($teams);
for($i=0; $i<=$count; $i++){
  if ($i = 0) {
  $slot0 = $teams[$i];
}
  if ($i = 1) {
  $slot1 = $teams[$i];
}
  if ($i = 2) {
  $slot2 = $teams[$i];
}
  if ($i = 3) {
  $slot3 = $teams[$i];
}
  if ($i = 4) {
  $slot4 = $teams[$i];
}
  }
?>

but im still not sure how it will all work out here is the full script i have now

 

<?php


if(strpos($_SERVER["HTTP_HOST"], "grimhq.com") !== false){
require "stats/config.php";

function getConnJO($joDbht, $joDbun, $joDbpw, $joDbnm) {  
    $joConn = mysql_connect($joDbht, $joDbun, $joDbpw) or die('<br/>ERROR! - Failure connecting to the database!'); 
    $joConn = mysql_select_db($joDbnm, $joConn) or die('<br />ERROR! - Failure selecting the database!');  
} 

getConnJO($joDbht, $joDbun, $joDbpw, $joDbnm);
//global $prefix, $db, $dbi, $sitename;

$sql = "SELECT `server_name`, `player_names`, `player_teams`, `map_name`, `game_type`, `time`, `max_players`, `num_players`, `state`, `lastUpdate` FROM `".$tablepre."_servers` WHERE `id` =1";

$result = mysql_query($sql);
echo '<center><table class="ServerStats" style="background-color:transparent">';
//while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
$row = mysql_fetch_array($result, MYSQL_ASSOC);

$gameType = array(
  "Team Deathmatch"       => "TDM"   , 
  "Cooperative"           => "COOP"  , 
  "Team King of the Hill" => "TKOTH" , 
  "King of the Hill"      => "KOTH"  , 
  "Search and Destroy"    => "SD"    , 
  "Attack and Defend"     => "AD"    , 
  "Capture the Flag"      => "CTF"   , 
  "Flagball"              => "FB"    , 
  "Advance and Secure"    => "AAS"   , 
    "Conquer and Control"   => "CAC"   , 
  "Deathmatch"            => "DM" 
);
//begin math for player graph
$pmath = $row['num_players']/$row['max_players'];
$math1 = $pmath*10;
$math2 = $math1*10;
$math = "$math2%";

if ($row['num_players'] == 0){
$math = '0%';
$show111 = true;
}
if ($row['num_players'] == $row['max_players']) {
$math = '100%';
}
if ($row['num_players'] != 0){
$playernames = $row['player_names'];
$playernames = str_replace('_', '<br>', $playernames);
$show111 = False;
}else{
$playernames = "Server Empty";
}
$mapname2 = $row['map_name'];
$currplayer = $row['num_players'];
$maxplayer = $row['max_players'];
$timeformated = format_time($row['time']);
$gametypearrayed = $gameType[$row['game_type']];
$mapname = base64_decode($row['map_name']);
$servername1 = $row['server_name'];
switch($row["state"]) {
    case 1: $state = "Loading"; break;
    case 3: $state = "<img src=\"http://fsflive.com/ambostats/images/nav_online.gif\"> Hosting"; break;
    case 4: $state = "Scoring"; break;
}
mysql_close();



//mysql connect 2
if ($dbc = @mysql_connect ('localhost','DBuser','pass')) {


   print"<!--Successful connection to mySQL-->\n";

                        mysql_select_db('DB');

                        //query
                        $query = "SELECT * FROM jostats_maps WHERE
                        (name = '".$mapname2."')";

                            if ($r = mysql_query ($query)) {
                            while ($row = mysql_fetch_array ($r)) {
                            $link = $row['id'];
                            }


                                     }else{
                         die ('<p>could not create the player because: <b>' . mysql_error() . "</b> The query was $query</p>");
                         }

                         mysql_close();
                         }else{
                         print'<p>could not connect to <b>mySQL</b>.</p>';
                         }



print "
<style type=text/css>
#percent {
position:relative;
bottom:22px;
}
.players {
white-space: nowrap;
  overflow: auto;
  max-height: 8em;
}
</style>";
//server name
echo "
<tr><td>ServerName: ".$servername1."</td></tr>";
//if a link is set than display as a link
if (isset($link)){
print "<tr><td>Map Name:<marquee behavior='scroll' direction='left' SCROLLAMOUNT=2 SCROLLDELAY=100><a href=\"http://grimhq.com/stats/index.php?&section=map_details&id=$link#jsTop\">".$mapname."</a></marquee></td></tr>";
}else{
//if not than display as static text
print "<tr><td>Map Name:<marquee behavior='scroll' direction='left' SCROLLAMOUNT=2 SCROLLDELAY=100>".$mapname."</marquee></td></tr>";
}
//continue with game type, time, players
print "<tr><td>MapType: ".$gametypearrayed."</td></tr>
<tr><td>TimeLeft: ".$timeformated."</td></tr>
<tr><td>Players: ".$currplayer."/".$maxplayer."</td></tr>";
//if not full
if ($show111 == False){
print "
<tr><td><center><table height=10 width=180 style=\"background-color: darkred\"><tr><td align=\"left\"><img src=\"http://grimhq.com/stats/tpls/grad1.png\" height=\"11\" width=\"$math\" /></center></td></tr></table></td></tr>
<tr><td><center><div id=percent>$math full</div></center></tr></td>";
}

print "<tr><td>State: ".$state."</td></tr>";
if ($show111 == False){
print "<tr><td>Players:<br><marquee height=\"100\" align=\"center\" behavior=\"scroll\" direction=\"up\" SCROLLAMOUNT=1 SCROLLDELAY=50 onmouseover=\"this.scrollAmount=0;\" onmouseout=\"this.scrollAmount=1;\">".$playernames."</marquee></td></tr>";
}


//}
echo "</table></center>";
} else {
  echo "cannot display locally";
}

function format_time($jotime1){
  $jotime = 1200 - $jotime1;
  $mins = floor($jotime/60);
  $secs = floor($jotime - ($mins * 60));
  if($secs < 10) $secs = $secs . "0";
  return "$mins:$secs";
}
?>

Link to comment
Share on other sites

ya that part is easy i know css and html well it is the php coding that i need help with

the part that generates the names.

i need help using explode to tell the php script what team each player is on.

plus making it print the names with the span

because right now as you can see

$playernames = str_replace('_', '<br>', $playernames);

witch means it prints

Lit.<br>The Fist<br>GRIM-TL-BB11<br>GRIM-KP1-PD<br>CHARLy-bR0WN<br>GRIM-S-M0NK3Y<br>GRIM-KP1-Dakilr<br>GRIM-M-LockOut<br>landsurveyor

Link to comment
Share on other sites

okay i fixed it with out much help but thanks for trying lol. i did use a explode function 2 explode both the player names and teams.

the end code looked like this.

<?php
$playernames = $row['player_names'];

$teams2 = $row['player_teams'];

$teams=explode("_", $teams2);

$nameplayers=explode("_", $playernames);

$count=count($teams);

print "<style type=text/css>
#percent {
position:relative;
bottom:22px;
}
.team1 {
color:#0E4AFF;
}
.team2 {
color:darkred;
}

</style>";

print "<tr><td>Players:<br><marquee height=\"100\" align=\"center\" behavior=\"scroll\" direction=\"up\" SCROLLAMOUNT=1 SCROLLDELAY=50 onmouseover=\"this.scrollAmount=0;\" onmouseout=\"this.scrollAmount=1;\">";
for($i=0; $i<=$count; $i++){
print "<br><span class=\"team$teams[$i]\">$nameplayers[$i]</span>";
}
print "</marquee></td></tr>";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.