Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    having a little issue with using functions

    so i am rather new to functions maybe you guys can help


    PHP Code:
    <?php
            $TP 
    '0';

    $offline '';
    $online '';

    //Hub
    //get_serverstat('','Hub');
    //Factions
    get_serverstat('67.169.244.236','Factions');
    //Creative
    get_serverstat('67.169.244.236:25555','Creative');
    //Kit PVP
    //get_serverstat('','Kit PVP');
    //Mini Games
    //get_serverstat('','Mini Games');

    echo '<font color=green>Online:</font><br>'.$online.'<font color=darkred>Offline:</font><br>'.$offline.'<br>';
    echo 
    '<b><font color=green>Total Players</font></b> - '.$tp;

    function 
    get_serverstat($serverip,$servername) {
            include_once 
    'status.php';
            
    $status = new MinecraftServerStatus();
            
    $response $status->getStatus($serverip);
            if(!
    $response) {
                if (
    $offline == '') {
                    
    $offline $servername.'</font> - 0/0 <br>';
                } else {
                    
    $offline $offline.$servername.'</font> - 0/0 <br>'; }
            } else { 
                
    $onlinestat ' - Players: '.$response['players'].'/'.$response['maxplayers'];
                if (
    $online == '') { 
                    
    $online $servername.'</font> '.$onlinestat.'<br>'
                } else { 
                    
    $online $servername.'</font> '.$onlinestat.'<br>'; }
                
    $tp $tp $response['players'];
            }

    }

    ?>
    I would just like this coded so it would show all the servers stats in the right areas
    Last edited by FishMonger; 04-07-2014 at 05:36 PM.

  • #2
    Regular Coder Linux_Sage's Avatar
    Join Date
    Mar 2014
    Location
    Sterling,VA
    Posts
    106
    Thanks
    0
    Thanked 10 Times in 10 Posts
    What are you getting any particular error messages?

  • #3
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok so what the problem is


    my function

    PHP Code:
    function get_serverstat($serverip,$servername) {
    include_once 
    'status.php';
    $status = new MinecraftServerStatus();
    $response $status->getStatus($serverip);
    if(!
    $response) {
    if (
    $offline == '') {
    $offline $servername.'</font> - 0/0 <br>';
    } else {
    $offline $offline.$servername.'</font> - 0/0 <br>'; }
    } else { 
    $onlinestat ' - Players: '.$response['players'].'/'.$response['maxplayers'];
    if (
    $online == '') { 
    $online $servername.'</font> '.$onlinestat.'<br>'
    } else { 
    $online $servername.'</font> '.$onlinestat.'<br>'; }
    $tp $tp $response['players'];

    i want to get the string values out side my function but i can not seem to get them - such as $online and $offline

    I am still rather green to coding php. BTW I am not getting any errors

    So how would i go about that
    Last edited by FishMonger; 04-07-2014 at 05:37 PM.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    That's a scope issue. Anything you've declared inside of a function is not visible outside of the function. You must capture the results using a return statement. You can return no more than one value, but that value can be an array or object as well, so you can capture each of these and return them.
    I don't understand what you are trying to do here though; the response string regardless of the branch it takes will be broken HTML.
    Oh wait, I see it now in the first post. Don't break these up like that, use either an entire function to handle the whole string, use applied formatting only when required, or use two separate functions one for data and one for format. Given what I see, what makes the most sense is a function responsible for both the data and the format.
    Frankly, the data is tabular, so you should use a table; perhaps something like this would be better?
    PHP Code:
    function get_serverstat($servers)
    {
        
    $mc = new MinecraftServerStatus();
        
    $iTotal 0;
        
    $sOut "
        <table>
            <thead>
                <tr>
                    <th>Status</th>
                    <th>Server</th>
                    <th>Players</th>
                </tr>
            </thead>
            <tbody>"
    ;

        foreach (
    $servers AS $ip => $name)
        {
            
    $out '';
            
    $status $mc->getStatus($ip);
            
    $sOut .= "<tr>";
            if (
    $status)
            {
                
    $sOut .= sprintf('<td style="color: green">ONLINE</td><td>%s</td><td>%d / %d</td>'$name$status['players'], $status['maxplayers']);
                
    $iTotal += $status['players'];
            }
            else
            {
                
    $sOut .= sprintf('<td style="color: darkred">OFFLINE</td><td>%s</td><td>0 / 0</td>'$name);
            }
            
    $sOut .= "</tr>";
        }
        
    $sOut .= "</tbody>";
        
    $sOut .= sprintf('
        <tfoot>
            <tr>
                <td colspan="2">Total Players:</td>
                <td>%s</td>
            </tr>
        </tfoot>'
    $iTotal);
        
    $sOut .= "</table>";
        return 
    $sOut;

    Called with:
    PHP Code:
    $servers = array(
        
    '67.169.244.236' => 'Factions',
        
    '67.169.244.236:25555' => 'Creative'
    );
    print 
    get_serverstat($servers); 
    An alternative would be to extend the current class and provide it with a function just for the above.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks with your help i manage to succeed to make what i wanted to do !

    http://iceycraft.org/forums/serverstat.php - this is what i made with what you gave me

    PHP Code:
            include_once 'status.php';
    function 
    get_serverstat($servers)
    {
        
    $mc = new MinecraftServerStatus();
        
    $iTotal 0;
        foreach (
    $servers AS $ip => $name)
        {
            
    $out '';
            
    $status $mc->getStatus($ip);
            if (
    $status)
            {
            
    $onlinestat ' - Players: '.$status['players'].'/'.$status['maxplayers'];
            
    $iTotal += $status['players']; 
                if (
    $online == '') {  
                    
    $online $name.'</font> '.$onlinestat.'<br>';          
                } else {
                    
    $online $online.$name.'</font> '.$onlinestat.'<br>'; } 
                } else {
                    if (
    $offline == '') { 
                
    $offline $name.'</font> - 0/0 <br>'
            } else { 
                
    $offline $offline.$name.'</font> - 0/0 <br>';
            } 
            }
        }
        echo 
    '<font color=green>Online:</font><br>'.$online.'<font color=darkred>Offline:</font><br>'.$offline.'<br>'
        echo 
    '<b><font color=green>Total Players</font></b> - '.$iTotal
    }
      

    $servers = array(
        
    ' ' => 'Hub',
        
    '67.169.244.236' => 'Factions',
        
    '67.169.244.236:25555' => 'Creative',
        
    '1.1.1.1' => 'Kit PVP',
        
    '4.3.2.1' => 'Mini Games',
    );
    print 
    get_serverstat($servers); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •