jmanfffreak Posted September 30, 2009 Share Posted September 30, 2009 Hi, I've been knee deep in documentation everywhere and I can't find how to do this.... I'm working on a server status script, with all the configs in an array. Here is the configuration file: <?php $server = array( '1' => array( 'ServerName' => 'Foo', 'ServerIP' => '127.0.0.1', 'ServerPort' => '9999', 'ServerDB' => 'Foo' ), '2' => array( 'ServerName' => 'Bar', 'ServerIP' => '127.0.0.1', 'ServerPort' => '9998', 'ServerDB' => 'Bar' ), ); ?> The enduser of this program will be able to add and subtract as many servers from this config as they wanted, all they'd have to do is copy the second array and change the array number from 1 to 2, or 2 to 3 or w/e. So it will need to loop through each array and return everything in order to make a function for it to do. The only problem is it has to loop and return each array as a seperate entity. In sense, the end results I want to look something like this: Server Status Foo: ONLINE Bar: OFFLINE Here's what I was thinking should be done, (aka this is what code I had) but unsure if it's right or what I should do: <?php function ServerStatus() { foreach ( $server as $val1 ) { foreach ( $val1 as $val2 ) { ........... } } } ?> But unsure of what to do next. Thanks for any help you can provide me. Quote Link to comment Share on other sites More sharing options...
cags Posted September 30, 2009 Share Posted September 30, 2009 As far as looping out the information goes, surely you just want something like... <?php echo '<b>Server Status:</b><br/>'; foreach($server as $s) { echo $s['ServerName'] . ': Status<br/>'; } ?> I'm not sure on how you plan on checking the status, is it a value in the array or is that the point of the question? Quote Link to comment Share on other sites More sharing options...
jmanfffreak Posted September 30, 2009 Author Share Posted September 30, 2009 Well, basically, I'll need to extract all the information in order to actually ping the server. I guess the server name could be the key in each array, so instead of what's above, I can omit the key 'ServerName' and instead use: <?php $server = array( 'Foo' => array( 'ServerIP' => '127.0.0.1', 'ServerPort' => '9999', 'ServerDB' => 'Foo' ), 'Bar' => array( 'ServerIP' => '127.0.0.1', 'ServerPort' => '9998', 'ServerDB' => 'Bar' ), ); ?> But pretty much I just need to know how to extract each individual array and use that information to plug into a function that will check the server status by a simple ping. I know how to do that part, I just don't know how to get the information I need to do it out of the Multidimensional array. Thanks. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted September 30, 2009 Share Posted September 30, 2009 <?php $server = array( 'Foo' => array( 'ServerIP' => '127.0.0.1', 'ServerPort' => '9999', 'ServerDB' => 'Foo' ), 'Bar' => array( 'ServerIP' => '127.0.0.1', 'ServerPort' => '9998', 'ServerDB' => 'Bar' ), ); ?> Cags got it right, didn't you test the code? echo '<pre>'; foreach($server as $s) { print_r($s, true); echo "\r\n"; } echo '</pre>'; Quote Link to comment Share on other sites More sharing options...
cags Posted September 30, 2009 Share Posted September 30, 2009 There are lots of ways you can access various information... <?php // access one specific servers information $current_server_id = 'Foo'; $current_server = $server[$current_server_id]; $current_server_ip = $current_server['ServerIP']; // loop through servers foreach($server as $k=>$s) { $current_server_id = $k; $current_server = $s; $current_server_ip = $s['ServerIP']; } ?> Does that help? Quote Link to comment Share on other sites More sharing options...
RussellReal Posted September 30, 2009 Share Posted September 30, 2009 <?php $test = socket_create(AF_INET,SOCK_STREAM); while ($e = each($server)) { $status = 'off'; if (socket_connect($test,$e['serverIP'],$e['serverPort'])) $status = 'on'; echo "{$e['serverName']} {$status}line"; } ?> oh I'd also like to note I am not completely sure you can reuse a socket resource but this one tries to atleast, tell me if you get any errors. Quote Link to comment Share on other sites More sharing options...
jmanfffreak Posted September 30, 2009 Author Share Posted September 30, 2009 There are lots of ways you can access various information... <?php // access one specific servers information $current_server_id = 'Foo'; $current_server = $server[$current_server_id]; $current_server_ip = $current_server['ServerIP']; // loop through servers foreach($server as $k=>$s) { $current_server_id = $k; $current_server = $s; $current_server_ip = $s['ServerIP']; } ?> Does that help? That....looks about right. Plugging it in now, I'll edit/repost with information and results. Thanks for all the input! Quote Link to comment 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.