Jump to content

Archived

This topic is now archived and is closed to further replies.

ajsuk

[Solved]Server Status Display Help?

Recommended Posts

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:
[code]
<?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>';
}

?>
[/code]

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.

Share this post


Link to post
Share on other sites
Couldn't you use that for() loop inside of a while() loop?

Share this post


Link to post
Share on other sites
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:

[code]<?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;
}

?>[/code]

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.

Share this post


Link to post
Share on other sites
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.

Share this post


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

Share this post


Link to post
Share on other sites
You just use the array like so:
[code]Server 1 is: <?=$status_array['1']?>[/code]

Which would output "OPEN", "LOCKED", or "CLOSED"

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
unless ive missed something, your original code practically does this, you just need to alter the echo statement:

[code]
<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>
[/code]

Share this post


Link to post
Share on other sites
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:
[code]$data = explode('|',$server_data[$i]);

To ->

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

Share this post


Link to post
Share on other sites
Ah ok, well there isn't that much you can do to make it all that shorter, but try this:

[code]
<?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;
}
}

?>
[/code]

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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

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.