dumb2champ Posted October 16, 2014 Share Posted October 16, 2014 Help me!! Seem when i run the scripts, the output seems wrong This is the output... The 1st and 3rd entry is true, online But the rest should be OFFLINE but the output shows all ONLINE I think the problem is from foreach loop but I cannot figure out how to solve the problem Please help me... <?php include 'connector/connect.php'; ?> <meta http-equiv="refresh" content="6" > <style> tbody > tr:nth-child(2n+1) > td, tbody > tr:nth-child(2n+1) > th { background-color: #ededed; } table{ width: 70%; margin: auto; border-collapse: collapse; box-shadow: darkgrey 3px; } thead tr { background-color: #36c2ff; } </style> <h1 align="center">Server Availability</h1> <table border="1"> <thead> <tr> <th>Entry</th> <th>Host</th> <th>Name</th> <th>Location</th> <th>Description</th> <th>Availability</th> </tr> </thead> <tbody> <?php $sql = "SELECT * FROM host_entry"; $no = 1; $dbhi = $dbh->query($sql); foreach ($dbhi->fetchAll(PDO::FETCH_BOTH) as $data) { ?> <tr> <td><?php echo $no++; ?></td> <td><?php echo $data['host_server']; ?></td> <td><?php echo $data['host_name']; ?></td> <td><?php echo $data['host_location']; ?></td> <td><?php echo $data['host_desc']; ?></td> <td> <?php $ip = $data["host_server"]; // Run the ping to the IP exec ("ping -n 1 -w 1 $ip", $ping_output); if(preg_match("/Reply/i", $ping_output[2])) { echo "<font color='green'><strong>Online!</strong></font>"; }else{ echo "<font color='red'><strong>Offline!</strong></font>"; } ?> </td> </tr> <?php } ?> </tbody> </table> Quote Link to comment https://forums.phpfreaks.com/topic/291866-php-host-status-from-php-mysql-database/ Share on other sites More sharing options...
Solution Barand Posted October 16, 2014 Solution Share Posted October 16, 2014 (edited) Clear the output array before each ping // Run the ping to the IP $ping_output = array(); exec ("ping -n 1 -w 1 $ip", $ping_output); Edited October 16, 2014 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/291866-php-host-status-from-php-mysql-database/#findComment-1493923 Share on other sites More sharing options...
dumb2champ Posted October 16, 2014 Author Share Posted October 16, 2014 Barrand thanks alot...it takes me 2 days to figure out what the problem are but i`m to weak...God...thanks for your help its solve now... Quote Link to comment https://forums.phpfreaks.com/topic/291866-php-host-status-from-php-mysql-database/#findComment-1493925 Share on other sites More sharing options...
Barand Posted October 16, 2014 Share Posted October 16, 2014 Some simple debugging on your part would have saved you two days of misery. Had you placed something like this to check the results echo '<pre>',print_r($ping_output, true),'</pre>'; after each ping you would have seen for yourself what was happening. Quote Link to comment https://forums.phpfreaks.com/topic/291866-php-host-status-from-php-mysql-database/#findComment-1493932 Share on other sites More sharing options...
dumb2champ Posted October 17, 2014 Author Share Posted October 17, 2014 Barand, the scripts work great.. I want to ask you, does my exac command in foreach..loop work at right way or not?? What i mean was, I want to create a ping sites that display status of ip/server... It include other computer, servers, website, and so on... So i use this this exac command to ping to other ip/server...if ok...could you suggest we or give examples of well writing exac command in foreach?? if not, its ok...i`m just asking.. Quote Link to comment https://forums.phpfreaks.com/topic/291866-php-host-status-from-php-mysql-database/#findComment-1493961 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.