Jimmy85 Posted July 1, 2022 Share Posted July 1, 2022 Hi, Basically I have written this PHP script to ping address on my network and then I would like to add the name that defines that IP after my image if successful or not. I am struggling on the name part can anyone assist? My code is below: <?php $systems = array("192.168.9.254","192.168.9.205","192.168.9.200","192.168.9.201","192.168.9.1","192.168.9.2","192.168.9.3","192.168.9.4","192.168.9.7","192.168.9.13"); $Sysname = array("Item1","Item2","Item3","Item4","Item5","Item6","Item7","Item8","Item9"); $good = "Received = 1"; $successValue; //echo "<h1>Site Status ".date("h:i:s")."</h1>"; echo "<br><br>"; foreach ($systems as $ip) { unset($result); $successValue = "DOWN"; exec("ping -n 1 $ip", $result); foreach($result as $line) { if (strpos($line,$good) == TRUE){ $successValue = "UP"; } } echo "IP Address: ".$ip." "; If ($successValue == "UP") { echo "System is ".$successValue. " "."<img src='/Images/GTick.jpg'>"." "; }else{ echo "System is ".$successValue. " "."<img src='/Images/RTick.jpg'>"." "; } echo "<br><br>"; //var_dump($result); } ?> Cheers James Quote Link to comment Share on other sites More sharing options...
Solution cyberRobot Posted July 1, 2022 Solution Share Posted July 1, 2022 I would switch to a multidimensional associative array for your data. For example, you could have something like this <?php $systems = array( array('ip'=>'192.168.9.254', 'name'=>'Item1'), array('ip'=>'192.168.9.205', 'name'=>'Item2'), //...remaining systems ); ?> That way all your data is in the same place. Then your loop could use the values like this: <?php foreach ($systems as $currSystem) { echo $currSystem['ip']; echo $currSystem['name']; } ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted July 1, 2022 Share Posted July 1, 2022 I was going to suggest combining the arrays $systems = array_combine($systems, $Sysname); then you could foreach ($systems as $ip => $name) { // ping } However, to that that you would need the same number of names as there are IPs. It would give Array ( [192.168.9.254] => Item1 [192.168.9.205] => Item2 [192.168.9.200] => Item3 [192.168.9.201] => Item4 [192.168.9.1] => Item5 [192.168.9.2] => Item6 [192.168.9.3] => Item7 [192.168.9.4] => Item8 [192.168.9.7] => Item9 [192.168.9.13] => Item10 ) Quote Link to comment 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.