SitePoint Sponsor

User Tag List

Results 1 to 23 of 23
  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,747
    Mentioned
    65 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
    4,823
    Mentioned
    142 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.
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  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
    4,823
    Mentioned
    142 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());  
                    }  
            } 
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  7. #7
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay. I see... But I need something like this though, because the script works by an array.

    PHP Code:
    $servers = array();   
    while(
    $row mysql_fetch_assoc($query)) {   
     
    array_push($servers"{$row['serverIP']}");   

    EDIT: Read below...
    Last edited by Craytor; Feb 11, 2013 at 14:34. Reason: See reply below this.

  8. #8
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually I'm not sure, is there a way to tell?

    EDIT: It seems it's working, it just won't display them here: http://mcss-portal.com/beta/serverstatus/demo/ (but that is okay.) On the main project http://mcss-portal.com/beta/. If you go to the demo page (refresh it) the data values will update on the main page (/beta/).

    So this means its working?
    Last edited by Craytor; Feb 11, 2013 at 14:20. Reason: Updating my text.

  9. #9
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,823
    Mentioned
    142 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Craytor View Post
    Okay. I see... But I need something like this though, because the script works by an array.

    PHP Code:
    $servers = array();   
    while(
    $row mysql_fetch_assoc($query)) {   
     
    array_push($servers"{$row['serverIP']}");   

    Not really, the array was redundant because your while loop was already providing that functionality.

    Quote Originally Posted by Craytor View Post
    Actually I'm not sure, is there a way to tell?

    EDIT: It seems it's working, it just won't display them here: http://mcss-portal.com/beta/serverstatus/demo/ (but that is okay.) On the main project http://mcss-portal.com/beta/. If you go to the demo page (refresh it) the data values will update on the main page (/beta/).

    So this means its working?
    Since we do not have the display code here, I can't tell you what the problem might be for that. What I can tell you, is if you are seeing the updates to the table working correctly, then I have solved your initial problem.
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  10. #10
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, well I want to thank you greatly. If I have problems in the future, do threads usually get answered in decent amount of time? I think you were fantastic with explaining whats wrong, and the response time. I would of responded earlier (previous replies) but I was at school.

    Thanks again!

  11. #11
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,823
    Mentioned
    142 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Craytor View Post
    Okay, well I want to thank you greatly. If I have problems in the future, do threads usually get answered in decent amount of time? I think you were fantastic with explaining whats wrong, and the response time. I would of responded earlier (previous replies) but I was at school.

    Thanks again!
    Depends who is on, but here at Sitepoint, there is usually several members more than capable at helping you with problems. The best way to get your questions answered, is 1) make sure you clearly present your question, 2) provide code show casing the problem (when possible), 3) be kind and be patient, some problems take time to resolve.

    Don't hesitate to ask questions here, there are a lot of smart people who browse these forums.
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  12. #12
    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!

  13. #13
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,823
    Mentioned
    142 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=\"#\">" 
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  14. #14
    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.

  15. #15
    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.

  16. #16
    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!

  17. #17
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,823
    Mentioned
    142 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?
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  18. #18
    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.

  19. #19
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,823
    Mentioned
    142 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?
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  20. #20
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    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?
    I am getting this: array(3) { ["usr"]=> string(7) "craytor" ["id"]=> string(1) "1" ["rememberMe"]=> int(1) }



    This is how I am defining the session...
    PHP Code:
    if($_POST['submit']=='Login')
    {
        
    // Checking whether the Login form has been submitted
        
        
    $err = array();
        
    // Will hold our errors
        
        
        
    if(!$_POST['username'] || !$_POST['password'])
            
    $err[] = 'All the fields must be filled in!';
        
        if(!
    count($err))
        {
            
    $_POST['username'] = mysql_real_escape_string($_POST['username']);
            
    $_POST['password'] = mysql_real_escape_string($_POST['password']);
            
    $_POST['rememberMe'] = (int)$_POST['rememberMe'];
            
            
    // Escaping all input data

            
    $row mysql_fetch_assoc(mysql_query("SELECT id,usr FROM users WHERE usr='{$_POST['username']}' AND pass='".md5($_POST['password'])."'"));

            if(
    $row['usr'])
            {
                
    // If everything is OK login
                
                
    $_SESSION['usr']=$row['usr'];
                
    $_SESSION['id'] = $row['id'];
                
    $_SESSION['rememberMe'] = $_POST['rememberMe'];
                
                
    // Store some data in the session
                
                
    setcookie('tzRemember',$_POST['rememberMe']);
            }
            else 
    $err[]='Wrong username and/or password!';
        }
        
        if(
    $err)
        
    $_SESSION['msg']['login-err'] = implode('<br />',$err);
        
    // Save the error messages in the session

        
    header("Location: login.php");
        exit;
    } .... 
    // goes on and on 

  21. #21
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,823
    Mentioned
    142 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Craytor View Post
    I am getting this: array(3) { ["usr"]=> string(7) "craytor" ["id"]=> string(1) "1" ["rememberMe"]=> int(1) }
    So that means the session is set, so now I'm a bit confused as to what your question is really trying to imply. The above output shows $_SESSION has a key "usr" which identifies it as being set, are there situations you expected it to NOT be set and your code is considering it set?
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  22. #22
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Heres what I am trying to do.

    IF session is set... show the <div class="row"></div> else if it isnt, show <div id="error"></div>

    But, when I go to access this page, and I am not logged in, <div class="row"></div> is being displayed and it isnt suppose to be.

    Example of what I dont want to happen: http://mcss-portal.com/beta/account.php

  23. #23
    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
  •