Can a value in an array have a label that isnt its value?

Ask about a PHP problem here.
Post Reply
gundofox
Posts: 4
Joined: Fri Jul 22, 2011 4:09 pm

Can a value in an array have a label that isnt its value?

Post by gundofox »

Hi,

I have just followed the Minecraft Server Status tutorial and it is all working as expected, (great videos - I have never used php before and yet I have managed to just about fudge through this afternoon with lots of rewinding/replaying of the video) however Ive now got a tiny little problem I hope you/someone may be able to help with...

My webserver is on a linux server on my own network, therefore when the server status page checks my own minecraft server using its local address all is well and it shows as online like it should. But, if I change its array value to my dyndns url then the server shows as offline as expected.

Is there a way to make the value of my server in the array its local ip address whilst giving it a label that is displayed in the html table that is the URL that the rest of the world would use?

Ive tried checking the syntax page on php.net, but couldnt seem to see anything that would help here, but then again like I said, I am literally only hours into my php experience!

Cheers,
Mark
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Can a value in an array have a label that isnt its value

Post by jacek »

You would have to change the structure of the code a bit to do that, adding another element to the array that contains the displayname would be easiest.

Can you post the code ?
Image
gundofox
Posts: 4
Joined: Fri Jul 22, 2011 4:09 pm

Re: Can a value in an array have a label that isnt its value

Post by gundofox »

Hiya, thanks for the reply so soon.

Here is my init.inc.php :
<?php

$servers = array(
	array('collinsworld.ath.cx', '25565'),
	array('172.16.0.79', '25565'),
);

error_reporting(0);

//$path = dirname(___FILE___);

include('minecraft_server.inc.php');
?>
I basically want the 2nd array to use 172.16.0.79 as the actual value, but have it display gundofox.dyndns.org in the table on the webpage.

I was thinking I could try and amend it to not use a loop and do it all longhand, but if theres a display name feature that would be great.

Thanks a lot,
Mark
gundofox
Posts: 4
Joined: Fri Jul 22, 2011 4:09 pm

Re: Can a value in an array have a label that isnt its value

Post by gundofox »

Also, not sure if you would want to see this bit aswell from the status.php :
<tbody>
				<?php

				foreach ($servers as $server){
					$online = server_online($server[0], $server[1]);

					?>	
					<tr>	
						<td><?php echo $server[0], ':', $server[1]; ?></td>
						<td class="<?php echo ($online) ? 'online' : 'offline'; ?>"><?php echo ($online) ? 'Online' : 'Offline'; ?></td>
					</tr>
					<?php
					}
					?>
			</tbody>
Thanks!
Mark
gundofox
Posts: 4
Joined: Fri Jul 22, 2011 4:09 pm

Re: Can a value in an array have a label that isnt its value

Post by gundofox »

Morning...

Im not sure why I didnt realise this at first... just re-read your reply about using an extra element and Ive sorted it now.
I just added a 3rd value which was the URL ([2]) and left the $online variable to use elements [0] & [1] but then used [2] & [1] for the actual table values. This has worked a treat!

Thanks a lot again,

Mark
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Can a value in an array have a label that isnt its value

Post by jacek »

gundofox wrote:Im not sure why I didnt realise this at first... just re-read your reply about using an extra element and Ive sorted it now.
I just added a 3rd value which was the URL ([2]) and left the $online variable to use elements [0] & [1] but then used [2] & [1] for the actual table values. This has worked a treat!
Exactly what I meant, good job :D
Image
Post Reply