SitePoint Sponsor

User Tag List

Results 1 to 23 of 23

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Problems, really this hard?

    Hello!

    I have a script and I am trying to get up time into the database. I dont know why, but when my server is offline, it still adds one. NOTE: all of the other features with offline work, and are entered correctly into the database. Any help would be greatly appreciated.

    PHP Code:
     $query mysql_query("SELECT * FROM servers") or die(mysql_error()); 
    $servers = array();  
    while(
    $row mysql_fetch_assoc($query)) {  
     
    array_push($servers"{$row['serverIP']}");  


    $query mysql_query("SELECT * FROM servers") or die(mysql_error()); 
            while(
    $post mysql_fetch_assoc($query)){ 
                
    $serverActualUptime $post['serverActualUptime']; 
                
    $serverPossibleUptime $post['serverPossibleUptime']; 
                foreach(
    $servers as $server) { 
                    
    $stats = \Minecraft\Stats::retrieve(new \Minecraft\Server($server)); 
                    if(
    $stats->is_online){ 
                        
    $serverStatus 1
                        
    $serverMOTD $stats->motd
                        
    $server
                        
    $serverPOnline $stats->online_players
                        
    $serverMPlayers $stats->max_players
                        
    $serverVersion $stats->game_version
                        
    $uptimeClass1 $serverActualUptime 1
                        
    $uptimeClass2 $serverPossibleUptime 1
                        
    $uptimePercentPart $uptimeClass1 $uptimeClass2
                        
    $uptimePercent $uptimePercentPart 100
                        
    $queryUpdate mysql_query("UPDATE servers SET serverStatus='$serverStatus', serverMessage='$serverMOTD', serverPlayersOnline='$serverPOnline', serverPlayersTotal='$serverMPlayers', serverVersion='$serverVersion', serverUptime='$uptimePercent', serverActualUptime='$uptimeClass1', serverPossibleUptime='$uptimeClass2' WHERE serverIP='$server'") or die(mysql_error()); 
                    } else { 
                        
    $serverStatus 0
                        
    $serverMOTD '?'
                        
    $server
                        
    $serverPOnline '?'
                        
    $serverMPlayers '?'
                        
    $serverVersion '?'
                        
    $uptimeClass3 $serverActualUptime 0
                        
    $uptimeClass4 $serverPossibleUptime 1
                        
    $uptimePercentPart $uptimeClass3 $uptimeClass4
                        
    $uptimePercent $uptimePercentPart 100
                        
    $queryUpdate mysql_query("UPDATE servers SET serverStatus='$serverStatus', serverMessage='$serverMOTD', serverPlayersOnline='$serverPOnline', serverPlayersTotal='$serverMPlayers', serverVersion='$serverVersion', serverUptime='$uptimePercent', serverActualUptime='$uptimeClass3', serverPossibleUptime='$uptimeClass4' WHERE serverIP='$server'") or die(mysql_error()); 
                    } 
                } 
            } 
    Again, even though the server is offline it is still adding a one to ActualUptime, and I need it added to PossibleUptime only!

    If you can help please do so!

    Thanks,
    TJ
    Last edited by Craytor; Feb 10, 2013 at 21:13. Reason: Title fix.

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    Is ServerStatus getting set to 0?
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes. In the database it sets to 0 and question marks are put where they belong.

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,169
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Craytor View Post
    Yes. In the database it sets to 0 and question marks are put where they belong.
    It stands to reason that $stats->is_online is true when you expected it to be false. Start there, as from looking at your code, I see no reason for it to run the wrong logic at this point.

  5. #5
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could it be me declaring the variables from the database as $serversctualuptime as that needs to be the uptime it pulls from the database for only the server that is being tested. I think it's getting it from a different server in the database.

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,169
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    *sigh*, I see it now

    Let's look at your code in parts, then it may make sense

    First you build an array of servers (though I have no idea why -- more on that later)
    PHP Code:
     $query mysql_query("SELECT * FROM servers") or die(mysql_error());  
    $servers = array();   
    while(
    $row mysql_fetch_assoc($query)) {   
     
    array_push($servers"{$row['serverIP']}");   

    Next you query ALL servers (again) and loop through them, grabbing the Actual and Possible up times
    PHP Code:
    $query mysql_query("SELECT * FROM servers") or die(mysql_error());  
            while(
    $post mysql_fetch_assoc($query)){  
                
    $serverActualUptime $post['serverActualUptime'];  
                
    $serverPossibleUptime $post['serverPossibleUptime']; 
    Then you loop through each server in the array you built at the top of your script. For each server in this array, you are apply the Actual and Possible uptime of the individual server you fetched above.
    PHP Code:
                foreach($servers as $server) {  
                    
    $stats = \Minecraft\Stats::retrieve(new \Minecraft\Server($server));  
                    if(
    $stats->is_online){  
                        
    $serverStatus 1;  
                        
    $serverMOTD $stats->motd;  
                        
    $server;  
                        
    $serverPOnline $stats->online_players;  
                        
    $serverMPlayers $stats->max_players;  
                        
    $serverVersion $stats->game_version;  
                        
    $uptimeClass1 $serverActualUptime 1;  
                        
    $uptimeClass2 $serverPossibleUptime 1;  
                        
    $uptimePercentPart $uptimeClass1 $uptimeClass2;  
                        
    $uptimePercent $uptimePercentPart 100;  
                        
    $queryUpdate mysql_query("UPDATE servers SET serverStatus='$serverStatus', serverMessage='$serverMOTD', serverPlayersOnline='$serverPOnline', serverPlayersTotal='$serverMPlayers', serverVersion='$serverVersion', serverUptime='$uptimePercent', serverActualUptime='$uptimeClass1', serverPossibleUptime='$uptimeClass2' WHERE serverIP='$server'") or die(mysql_error());  
                    } else {  
                        
    $serverStatus 0;  
                        
    $serverMOTD '?';  
                        
    $server;  
                        
    $serverPOnline '?';  
                        
    $serverMPlayers '?';  
                        
    $serverVersion '?';  
                        
    $uptimeClass3 $serverActualUptime 0;  
                        
    $uptimeClass4 $serverPossibleUptime 1;  
                        
    $uptimePercentPart $uptimeClass3 $uptimeClass4;  
                        
    $uptimePercent $uptimePercentPart 100;  
                        
    $queryUpdate mysql_query("UPDATE servers SET serverStatus='$serverStatus', serverMessage='$serverMOTD', serverPlayersOnline='$serverPOnline', serverPlayersTotal='$serverMPlayers', serverVersion='$serverVersion', serverUptime='$uptimePercent', serverActualUptime='$uptimeClass3', serverPossibleUptime='$uptimeClass4' WHERE serverIP='$server'") or die(mysql_error());  
                    }  
                }  
            } 
    See the problem? You are applying the Actual and Possible uptime of one server across ALL servers, then you move to the next server and do it again, and again, and again until your nested loops finish.

    Here is what I think you intended to do:
    First remove the servers array, you don't need it. Your while loop does it for you.
    Remove the foreach, as now that you don't have the array, you don't need that either. All you need to do is send the serverIP to the Minecraft function.
    Which leaves you with the following:
    PHP Code:
    $query mysql_query("SELECT * FROM servers") or die(mysql_error());  
            while(
    $post mysql_fetch_assoc($query)){  
                
    $serverActualUptime $post['serverActualUptime'];  
                
    $serverPossibleUptime $post['serverPossibleUptime'];  

                    
    $stats = \Minecraft\Stats::retrieve(new \Minecraft\Server($post['serverIP']));  
                    if(
    $stats->is_online){  
                        
    $serverStatus 1;  
                        
    $serverMOTD $stats->motd;  
                        
    $server;  
                        
    $serverPOnline $stats->online_players;  
                        
    $serverMPlayers $stats->max_players;  
                        
    $serverVersion $stats->game_version;  
                        
    $uptimeClass1 $serverActualUptime 1;  
                        
    $uptimeClass2 $serverPossibleUptime 1;  
                        
    $uptimePercentPart $uptimeClass1 $uptimeClass2;  
                        
    $uptimePercent $uptimePercentPart 100;  
                        
    $queryUpdate mysql_query("UPDATE servers SET serverStatus='$serverStatus', serverMessage='$serverMOTD', serverPlayersOnline='$serverPOnline', serverPlayersTotal='$serverMPlayers', serverVersion='$serverVersion', serverUptime='$uptimePercent', serverActualUptime='$uptimeClass1', serverPossibleUptime='$uptimeClass2' WHERE serverIP='$server'") or die(mysql_error());  
                    } else {  
                        
    $serverStatus 0;  
                        
    $serverMOTD '?';  
                        
    $server;  
                        
    $serverPOnline '?';  
                        
    $serverMPlayers '?';  
                        
    $serverVersion '?';  
                        
    $uptimeClass3 $serverActualUptime 0;  
                        
    $uptimeClass4 $serverPossibleUptime 1;  
                        
    $uptimePercentPart $uptimeClass3 $uptimeClass4;  
                        
    $uptimePercent $uptimePercentPart 100;  
                        
    $queryUpdate mysql_query("UPDATE servers SET serverStatus='$serverStatus', serverMessage='$serverMOTD', serverPlayersOnline='$serverPOnline', serverPlayersTotal='$serverMPlayers', serverVersion='$serverVersion', serverUptime='$uptimePercent', serverActualUptime='$uptimeClass3', serverPossibleUptime='$uptimeClass4' WHERE serverIP='$server'") or die(mysql_error());  
                    }  
            } 

  7. #7
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another question:
    PHP Code:
    function getNavButtonDrop() {
         if(isset(
    $_SESSION['usr'])){
             echo 
    "<div class=\"btn-group\">";
             echo 
    "<a class=\"btn dropdown-toggle\" data-toggle=\"dropdown\" href=\"#\">"
             
    echo "<i class=\"icon-user\"></i>" $_SESSION['usr'];
             echo 
    "<span class=\"caret\"></span>";
             echo 
    "</a>";
             echo 
    "<ul class=\"dropdown-menu\">";
             echo 
    "<li><a href=\"#\">Login</a></li>";
             echo 
    "<li><a href=\"#\">Register</a></li>";
             echo 
    "</ul>";
             echo 
    "</div>";
         }else{
             echo 
    "<div class=\"btn-group\">";
             echo 
    "<a class=\"btn dropdown-toggle\" data-toggle=\"dropdown\" href=\"#\">"
             
    echo "<i class=\"icon-user\"></i> Account"
             echo 
    "<span class=\"caret\"></span>";
             echo 
    "</a>";
             echo 
    "<ul class=\"dropdown-menu\">";
             echo 
    "<li><a href=\"#\">Login</a></li>";
             echo 
    "<li><a href=\"#\">Register</a></li>";
             echo 
    "</ul>";
             echo 
    "</div>";
         }

    I am getting an error with "Parse error: syntax error, unexpected T_ECHO, expecting ',' or ';' in /home/mcssport/public_html/beta/functions.php on line 192".

    Line 192 is echo "<i class=\"icon-user\"></i>" . $_SESSION['usr'];

    Help would be greatly appreciated!

  8. #8
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,169
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Missing semi-colon on
    PHP Code:
             echo "<a class=\"btn dropdown-toggle\" data-toggle=\"dropdown\" href=\"#\">" 
    and (in the else)
    PHP Code:
             echo "<a class=\"btn dropdown-toggle\" data-toggle=\"dropdown\" href=\"#\">" 

  9. #9
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The server status is set to 0, thus whenever it starts, it gets incremented by one.

  10. #10
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by spencerharry80 View Post
    The server status is set to 0, thus whenever it starts, it gets incremented by one.
    Dont get the random post, but we have already been over this! Thanks!

  11. #11
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    Missing semi-colon on
    PHP Code:
             echo "<a class=\"btn dropdown-toggle\" data-toggle=\"dropdown\" href=\"#\">" 
    and (in the else)
    PHP Code:
             echo "<a class=\"btn dropdown-toggle\" data-toggle=\"dropdown\" href=\"#\">" 
    Nice! Lol...

    Today... I cant seem to figure this one out. I am thinking that the HTML code would listen to the PHP, but I really don't think that is the case!
    PHP Code:
          <?php
                
                
    if(isset($_SESSION['usr'])){
                
                
    ?>

          <div class="row">
            <div class="span12" style="background-color: #ffffff;">
              
           </div>
          </div>
            <?php
                
                
    } else {
                
                
    ?>
                </br>
                </br>
                </br>
                </br>
                <div class="alert alert-error">
                  <b>Just not so fast.</b> You need to login before you can view this page!
                </div>
                <?php
                
    }
                
    ?>
    Note this is part of the page account.php.

  12. #12
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,169
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Craytor View Post
    Today... I cant seem to figure this one out. I am thinking that the HTML code would listen to the PHP, but I really don't think that is the case!
    PHP Code:
          <?php
                
                
    if(isset($_SESSION['usr'])){
                
                
    ?>

          <div class="row">
            <div class="span12" style="background-color: #ffffff;">
              
           </div>
          </div>
            <?php
                
                
    } else {
                
                
    ?>
                </br>
                </br>
                </br>
                </br>
                <div class="alert alert-error">
                  <b>Just not so fast.</b> You need to login before you can view this page!
                </div>
                <?php
                
    }
                
    ?>
    Note this is part of the page account.php.
    Care to explain the problem? Is it always showing the else?

  13. #13
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    Care to explain the problem? Is it always showing the else?
    Sure! Sorry about that... It always shows:
    <div class="row">
    <div class="span12" style="background-color: #ffffff;">

    </div>
    </div>
    and the else is only displayed when needed.

  14. #14
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,169
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Craytor View Post
    Sure! Sorry about that... It always shows:
    <div class="row">
    <div class="span12" style="background-color: #ffffff;">

    </div>
    </div>
    and the else is only displayed when needed.
    Where is your logic for setting $_SESSION['usr']? If you add <?php var_dump($_SESSION); ?> do you always see usr inside it when visiting the page?

  15. #15
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ha! I fixed it. Sorry for bothering you, and thanks for your help!


Bookmarks

Posting Permissions

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