The online racing simulator
foreach in php
Hello. Does anyone know, is there foreach loop with connection in PHP?
In C# :
foreach (var C in connections.Values)
{
MessageToAll(C.PName);
}

And it outputs

Nickname1
Other-player
Player
etc...

Can anyone post this code in PRISM?
I know, my english isn't best, if someone didn't understood what I want, I can examine more detally.

<?php 
    
foreach ($this->getHostState()->players as $plid => $player)
        
console($player->PName);
    
?>

This will always return the current players for the current server that PRISM is processing the packet from. As you can connect to multiple servers with the same instance of PRISM.


<?php 
    
foreach ($this->getHostState($hostID)->players as $plid => $player)
        
console($player->PName);
    
?>

With this code, you can get information about the host state for $hostID.


<?php 
    
foreach ($this->getHostState()->clients as $ucid => $client)
        
console($client->UName);
    
?>

Also works to get all clients using the same syntax.
When I copied Dygear code to plugin, I did get nothing - no error, no text with usernames.
Post the plugin, the whole thing, that you are attempting to use this in, or a striped down use case so we can see what you wrote.


<?php 
php
class test extends Plugins
{
    const 
URL 'http://lfsforum.net/forumdisplay.php?f=312';
    const 
NAME 'Test Code';
    const 
DESCRIPTION 'Just a plugin that I use to test code in.';
    const 
AUTHOR "Mark 'Dygear' Tomlin";
    const 
VERSION PHPInSimMod::VERSION;

    public function 
__construct()
    {
        
$this->registerSayCommand('test''cmdTest''The Code To Test.');
    }

    public function 
cmdTest($cmd$ucid)
    {
        foreach (
$this->getHostState()->players as $plid => $player)
            
console($player->PName);
    
        foreach (
$this->getHostState()->clients as $ucid => $client)
            
console($client->UName);

        return 
PLUGIN_HANDLED;
    }
}
?>

This is my code, installed in the PRISM/plugins/test.php, and I've activated the plugin in the config file as well.
Oops, I didn't seen different between

foreach ($this->getHostState($hostID)->[B][U]players[/U][/B] as [B][U]$plid[/U][/B] => $player)
console($player->PName);

and

foreach ($this->getHostState()->[B][U]clients[/U][/B] as [B][U]$ucid[/U][/B] => $client)
console($client->UName);

Thank you Dygear, now it is all working.

FGED GREDG RDFGDR GSFDG