Jimmy85 Posted July 4, 2022 Share Posted July 4, 2022 Hi, my table is built up of array items basically the array item is the table isn't like the others and i cannot work it out. (I have attached a image to show you what it looks like) Here is my code: Quote function CriticalSystem(){ //array for critical devices $systems = array( array('ip' => '192.168.9.254', 'name' => 'Tech Swtich'), array('ip' => '192.168.9.205', 'name' => 'Printer'), array('ip' => '192.168.9.200', 'name' => 'Sales Printer'), array('ip' => '192.168.9.201', 'name' => 'Admin Printer'), array('ip' => '192.168.9.1', 'name' => 'KVM'), array('ip' => '192.168.9.2', 'name' => 'Office Data 24 Port'), array('ip' => '192.168.9.3', 'name' => 'Office Data 48 Port'), array('ip' => '192.168.9.4', 'name' => 'Office Voice 48 Port'), array('ip' => '192.168.9.7', 'name' => 'Warehouse Switch'), array('ip' => '192.168.9.13', 'name' => 'Foundry Canteen Switch'), ); //Result to search for to give success result $good = "Received = 1"; $successValue; //troubleshooting to see if being re-freshed //echo "<h1>Site Status ".date("h:i:s")."</h1>"; // foreach loop to ping IP and check if alive or dead & dispaly result foreach ($systems as $ip) { unset($result); $successValue = "DOWN"; exec("ping -n 1 $ip[ip]", $result); foreach($result as $line) { if (strpos($line,$good) == TRUE){ $successValue = "UP"; } } //echo "<br><br>"; echo "<table class='table'>"; echo "<tbody>"; echo"<tr>"; echo"<td>"; echo "IP Address: ".$ip['ip']. " "."</td>"."<td>"."Unit Name: ".$ip['name']." "."</td>"; //echo "</td>"; echo"</td>"; echo"</tr>"; echo"<tr>"; echo"<td>"; If ($successValue == "UP") { //echo "<br><br>"; echo "Status is: ".$successValue." "."<td>"."<img src='/Images/GTick.jpg'>"."</td>"; }else{ //echo "<br><br>"; echo "Status is: ".$successValue." "."<img src='/Images/RTick.jpg'>"; } echo "</td>"; echo "</tr>"; echo "</tbody>"; echo"</table>"; //debug dispaly full result of ping request //var_dump($result); } } Cheers James Quote Link to comment https://forums.phpfreaks.com/topic/314996-first-array-item-doesnt-show-as-other-is-table/ Share on other sites More sharing options...
dodgeitorelse3 Posted July 4, 2022 Share Posted July 4, 2022 try this (untested) // foreach loop to ping IP and check if alive or dead & dispaly result foreach ($systems as $ip) { unset($result); $successValue = "DOWN"; exec("ping -n 1 $ip[ip]", $result); foreach($result as $line) { if (strpos($line,$good) == TRUE){ $successValue = "UP"; } } //echo "<br><br>"; echo "<table class='table'> <tbody> <tr> <td>IP Address: ".$ip['ip']. "</td> <tdUnit Name: ".$ip['name']."</td> </tr> <tr>>"; If ($successValue == "UP") { echo "<td>Status is: ".$successValue."</td> <td><img src='/Images/GTick.jpg'></td>"; }else{ echo "<td>Status is: ".$successValue."</td> <td><img src='/Images/RTick.jpg'></td>"; } echo "</tr> </tbody> </table>"; //debug dispaly full result of ping request //var_dump($result); } Quote Link to comment https://forums.phpfreaks.com/topic/314996-first-array-item-doesnt-show-as-other-is-table/#findComment-1597937 Share on other sites More sharing options...
Solution Barand Posted July 4, 2022 Solution Share Posted July 4, 2022 Te original html markup was inconsistent. I cleaned up the output too, but slightly differently. I couldn't see the point of separate tables (you can more easily guarantee consistency with a single one) echo "<table border='1' class='table'>"; // foreach loop to ping IP and check if alive or dead & dispaly result foreach ($systems as $ip) { unset($result); $successValue = "DOWN"; exec("ping -n 1 $ip[ip]", $result); foreach($result as $line) { if (strpos($line,$good) == TRUE){ $successValue = "UP"; } } echo "<tbody> <tr> <td>IP Address: {$ip['ip']}</td> <td>Unit Name: {$ip['name']}</td> </tr> <tr> <td>Status is: $successValue</td> <td>" . ($successValue == "UP" ? "<img src='/Images/GTick.jpg'>" : "<img src='/Images/RTick.jpg'>") . "</td> </tr> </tbody> "; } echo"</table>"; Quote Link to comment https://forums.phpfreaks.com/topic/314996-first-array-item-doesnt-show-as-other-is-table/#findComment-1597938 Share on other sites More sharing options...
Jimmy85 Posted July 5, 2022 Author Share Posted July 5, 2022 (edited) Thanks Barand, Is there anyway I could count the number of up services and down services i am currently doing some research on how to do this but coming up with blanks I appreciate the assistance so far. Edited July 5, 2022 by Jimmy85 Quote Link to comment https://forums.phpfreaks.com/topic/314996-first-array-item-doesnt-show-as-other-is-table/#findComment-1597968 Share on other sites More sharing options...
Barand Posted July 5, 2022 Share Posted July 5, 2022 Just create a counter variable and increment it in the loop $total_systems = count($systems); $total_up = 0; echo "<table border='1' class='table'>"; // foreach loop to ping IP and check if alive or dead & dispaly result foreach ($systems as $ip) { unset($result); $successValue = "DOWN"; exec("ping -n 1 $ip[ip]", $result); foreach($result as $line) { if (strpos($line,$good) == TRUE){ $successValue = "UP"; ++$total_up; // increment up count } } echo "<tbody> <tr> <td>IP Address: {$ip['ip']}</td> <td>Unit Name: {$ip['name']}</td> </tr> <tr> <td>Status is: $successValue</td> <td>" . ($successValue == "UP" ? "<img src='/Images/GTick.jpg'>" : "<img src='/Images/RTick.jpg'>") . "</td> </tr> </tbody> "; } $total_down = $total_systems - $total_up; echo "<tr><th>Systems UP</th> // output totals <td>$total_up</td> </tr> <tr><th>Systems DOWN</th> <td>$total_down</td> </tr> </table> "; Quote Link to comment https://forums.phpfreaks.com/topic/314996-first-array-item-doesnt-show-as-other-is-table/#findComment-1597973 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.