Jump to content

First Array Item doesn't show as other is table


Go to solution Solved by Barand,

Recommended Posts

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

 

Critical System.jpg

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);
    }

 

  • Solution

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>";

 

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 by Jimmy85

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>
          ";

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.