Jump to content


Photo

[Solved]Server Status Display Help?


  • Please log in to reply
10 replies to this topic

#1 ajsuk

ajsuk
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 13 July 2006 - 07:53 PM

Hey, I've been playing with a script that displays the status of some game servers.

Here it is, first off as given out by the game company:
<?php
////////////////////////////////////////////////////////////
// Insert this code in your page to get the server status //
////////////////////////////////////////////////////////////

// retrieve information from Ryzom's website
$server_data = file('http://atys.ryzom.com/serverstatus/status.php');

for ($i = 0; $i < sizeof($server_data); $i ++)
{
	$data = explode('|',$server_data[$i]);
	$server = $data[0];
	$status = 'CLOSED';
	switch($data[1])
	{
		case 1:  $status = 'OPEN'; break;
		case 2:  $status = 'LOCKED'; break;
	}
	echo $server .' - '. $status .'<br>';
}

?>

This is all well and good, it'll produce a list of all the servers along their status.
What I want to do though is format the results in tables etc in my page. I found I could edit the "$i" to "0", "1", "2" on so on to make it just produce results for one server and then repeat the entire code down the page for each server but that seems a little messy and overcomplicated. Is there a way I can edit it so that I can tell it to echo the _name_ and then the _status_ of each server without having almost the exact same code pasted all over the page?

Thanks in advance for any help!

-Andy.

#2 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 13 July 2006 - 08:09 PM

Couldn't you use that for() loop inside of a while() loop?

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#3 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 13 July 2006 - 08:22 PM

well without knowing exactly how you want it formatted, you could toss all the server info into an array with which you can echo the data anywhere on the page:

<?php
////////////////////////////////////////////////////////////
// Insert this code in your page to get the server status //
////////////////////////////////////////////////////////////

// retrieve information from Ryzom's website
$server_data = file('http://atys.ryzom.com/serverstatus/status.php');

$status_array = array();

foreach ($server_data AS $data)
{
  // explode the current server's info into useable chunks
  $pieces = explode('|', $data);

  // set the proper status
  switch ($pieces[1])
  {
    case 1: $status = 'OPEN'; break;
    case 2: $status = 'LOCKED'; break;
    default: $status = 'CLOSED'; break;
  }

  // plug the info into an easy-to-handle array for later use
  $status_array["{$pieces[0]}"] = $status;
}

?>

the info will then be in the format $status_array[server] = status, so you can use a foreach on it:

not too clear on how you want it formatted, but at any rate, there's my input.

#4 ajsuk

ajsuk
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 13 July 2006 - 11:14 PM

Sorry about that, I'll try to be more clear on how I want it to format the results.

When it prints the server info ("echo $server .' - '. $status .'<br >';") its just a row for each server with the status
Server one - UP
Server two - CLOSED
Server three - CLOSED

etc...

Now I can see how to seperate the server and the status with the echos "echo $server" or "echo $status" but not each set of them because all it'll do is print, start a new line and repeat.  ;)
Is there a way to group them in to a kind of set to that I can call for it to print results from whichever set, 1, 2, 3, 4 etc...
Like a echo $server from set #3 would print the server name from the 3rd set of results.

#5 ajsuk

ajsuk
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 14 July 2006 - 10:18 AM

I'm not too sure what your edited version is supposed to do, akitchin. Sorry I'm still fairly new at this stuff.  :(

#6 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 14 July 2006 - 10:50 AM

You just use the array like so:
Server 1 is: <?=$status_array['1']?>

Which would output "OPEN", "LOCKED", or "CLOSED"
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#7 ajsuk

ajsuk
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 14 July 2006 - 11:20 AM

That doesn't seem to do anything... Also that would just be the status, how do I get it to print the name of the server too?
The servers each have a name, not just server: one, two or three, that was just an example, the original code also prints the name.

#8 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 14 July 2006 - 11:25 AM

unless ive missed something, your original code practically does this, you just need to alter the echo statement:

<table>
<tr><td>Server</td><td>Status</td></tr>
<?php
////////////////////////////////////////////////////////////
// Insert this code in your page to get the server status //
////////////////////////////////////////////////////////////

// retrieve information from Ryzom's website
$server_data = file('http://atys.ryzom.com/serverstatus/status.php');

for ($i = 0; $i < sizeof($server_data); $i ++)
{
	$data = explode('|',$server_data[$i]);
	$server = $data[0];
	$status = 'CLOSED';
	switch($data[1])
	{
		case 1:  $status = 'OPEN'; break;
		case 2:  $status = 'LOCKED'; break;
	}
	echo "<tr><td>$server</td><td>$status</td></tr>";
}

?>
</table>


#9 ajsuk

ajsuk
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 14 July 2006 - 11:34 AM

hehe yeah, but I cant specify which set of results I want to go where in the page. Thats the aim of what I wanted it do.

With the original code it'll just print the entire list of results wherever I paste the $server &/or $server...
I want to be able to tell it to split up the results from each server as they come in so that I can place bits in table cells perhaps rather than the entire thing printing in one cell.  :)

As I mentioned before I found one way of doing this was to edit the:
$data = explode('|',$server_data[$i]);

To ->

$data = explode('|',$server_data[0]);
editing the $i to 0, 1, 2, 3 etc...
For whichever server but then I'd have to copy the entire code out again when I want a result from a different server.

#10 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 14 July 2006 - 11:42 AM

Ah ok, well there isn't that much you can do to make it all that shorter, but try this:

<?php
////////////////////////////////////////////////////////////
// Insert this code in your page to get the server status //
////////////////////////////////////////////////////////////

// retrieve information from Ryzom's website
$server_data = file('http://atys.ryzom.com/serverstatus/status.php');

for ($i = 0; $i < sizeof($server_data); $i ++)
{
	$data = explode('|',$server_data[$i]);
	$server[$i] = $data[0];
	$status[$i] = 'CLOSED';
	switch($data[1])
	{
		case 1:  $status[$i] = 'OPEN'; break;
		case 2:  $status[$i] = 'LOCKED'; break;
	}
}

?>

And then each server name can be accessed by $server[0],$server[1]and $server[2] whilst the status can be retrieved from $status[0],$status[1]and $status[2]

#11 ajsuk

ajsuk
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 14 July 2006 - 11:50 AM

Sweet! We're on the same page hehe, thats exactly what I wanted!
I don't mind having the code once on the page at all, it was having it over and over again everytime I wanted a result from a different server that was just silly, hehe.

Thanks alot!

;D




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users