ajsuk Posted July 13, 2006 Share Posted July 13, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/14524-solvedserver-status-display-help/ Share on other sites More sharing options...
pixy Posted July 13, 2006 Share Posted July 13, 2006 Couldn't you use that for() loop inside of a while() loop? Quote Link to comment https://forums.phpfreaks.com/topic/14524-solvedserver-status-display-help/#findComment-57555 Share on other sites More sharing options...
akitchin Posted July 13, 2006 Share Posted July 13, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/14524-solvedserver-status-display-help/#findComment-57561 Share on other sites More sharing options...
ajsuk Posted July 13, 2006 Author Share Posted July 13, 2006 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 statusServer one - UPServer two - CLOSEDServer three - CLOSEDetc...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. Quote Link to comment https://forums.phpfreaks.com/topic/14524-solvedserver-status-display-help/#findComment-57642 Share on other sites More sharing options...
ajsuk Posted July 14, 2006 Author Share Posted July 14, 2006 I'm not too sure what your edited version is supposed to do, akitchin. Sorry I'm still fairly new at this stuff. :( Quote Link to comment https://forums.phpfreaks.com/topic/14524-solvedserver-status-display-help/#findComment-57820 Share on other sites More sharing options...
ShogunWarrior Posted July 14, 2006 Share Posted July 14, 2006 You just use the array like so:[code]Server 1 is: <?=$status_array['1']?>[/code]Which would output "OPEN", "LOCKED", or "CLOSED" Quote Link to comment https://forums.phpfreaks.com/topic/14524-solvedserver-status-display-help/#findComment-57832 Share on other sites More sharing options...
ajsuk Posted July 14, 2006 Author Share Posted July 14, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/14524-solvedserver-status-display-help/#findComment-57844 Share on other sites More sharing options...
GingerRobot Posted July 14, 2006 Share Posted July 14, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/14524-solvedserver-status-display-help/#findComment-57845 Share on other sites More sharing options...
ajsuk Posted July 14, 2006 Author Share Posted July 14, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/14524-solvedserver-status-display-help/#findComment-57846 Share on other sites More sharing options...
GingerRobot Posted July 14, 2006 Share Posted July 14, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/14524-solvedserver-status-display-help/#findComment-57850 Share on other sites More sharing options...
ajsuk Posted July 14, 2006 Author Share Posted July 14, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/14524-solvedserver-status-display-help/#findComment-57855 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.