What did I do wrong with this code?
<p>My PHP scripts below should display a list of device status up or down. But even when one device is up it shows both down. What did I do wrong here in loop statement?</p>

<pre><code>    &lt;?
    require "config.php";
    include_once("header.php");

    if($_SESSION['UserId']==""){
        $msg="You need to be auhenticated or have privileges to be here";
        redirect("main.php");
    }

    $sql = ("SELECT * FROM ont");
    $query = db_query($sql);

    $sql1 = "SELECT ont_id,pon_port FROM ont";
    $query1 = db_query($sql1);

    ?&gt;

    &lt;head&gt;
    &lt;meta http-equiv="refresh" content="30" /&gt;
    &lt;/head&gt;

    &lt;br&gt;
    &lt;div align="center"&gt;&lt;strong&gt;&lt;a&gt;ONT List&lt;/a&gt;&lt;/strong&gt;&lt;/div&gt;
    &lt;table border="0" align="center"&gt;
    &lt;tr&gt;
    &lt;td&gt;S.N.&lt;/td&gt;
        &lt;td&gt;ONT ID&lt;/td&gt;
    &lt;td&gt;HostName&lt;/td&gt;
        &lt;td&gt;PON Port&lt;/td&gt;
        &lt;td&gt;Splitter&lt;/td&gt;
        &lt;td&gt;Splitter Port&lt;/td&gt;
        &lt;td&gt;Run State&lt;/td&gt;
        &lt;td&gt;Match State&lt;/td&gt;
        &lt;td&gt;Distance(m)&lt;/td&gt;
        &lt;td&gt;Status&lt;/td&gt;
        &lt;td&gt;
    &lt;/tr&gt;

    &lt;?
    $k=1;
    while($row=db_fetch_array($query)){
        $pon_port=$row['pon_port'];
        $ont_id=$row['ont_id']; 


//Query snmp for ONT RunStatus
    $text = shell_exec("/usr/local/bin/snmpwalk -v2c -c public 10.0.9.3 SNMPv2- SMI::enterprises.2011.6.128.1.1.2.57.1.15.".$pon_port.".".$ont_id."");
    $len =  preg_match_all('/INTEGER: (\d+)/',$text,$hits);
        for ( $i=0 ; $i&lt;$len ; $i++ ) {
        $value = $hits[1][$i];  
        //$string = $hits[2][$i];
        }
                if ($value == "1"){
                        $status=up;
                }else{
                        $status=down;
                }
/* END RUN Status */

//Query ONT Match condition 1= Initialization, 2 = Match, 3 = Mismatch
    $match_status = shell_exec("/usr/local/bin/snmpwalk -v2c -c public 10.0.9.3 SNMPv2- SMI::enterprises.2011.6.128.1.1.2.57.1.18.".$pon_port.".".$ont_id."");
    $match_reg = preg_match_all('/INTEGER: (\d+)/',$match_status,$hits);
                for ( $i=0; $i&lt;$match_reg; $i++ ){
                $value1 = $hits[1][$i];
                //$string1 = $hits[2][$i];
                }
                        if ($value1 == "1"){
                                $match_status=Initialization;
                        }
                        if ($value1 == "2"){
                                $match_status="Match";
                        }
                        if ($value1 == "3"){
                                $match_status="Mismatch";
                        }
/* END of Match */

// Query ONT Distance
    $distance = shell_exec("/usr/local/bin/snmpwalk -v2c -c public 10.0.9.3 SNMPv2-SMI::enterprises.2011.6.128.1.1.2.57.1.19.".$pon_port.".".$ont_id."");
    $dist_match = preg_match_all('/INTEGER: (\d+)/',$distance,$hits);
                for ( $i=0; $i&lt;$dist_match; $i++){
                        $ont_distance = $hits[1][$i];
                        }

/* END OF ONT Distance */

        if ($status == "up" &amp;&amp; $value1 == "2"){
                $image = "&lt;img height='16' src='images/green.png'&gt;";
                }else{
                $image = "&lt;img height='16' src='images/red.png'&gt;";
        }


    ?&gt;

  &lt;tr&gt;
        &lt;td bgcolor="&lt;? echo $color;?&gt;"&gt;&lt;? echo $k;?&gt;&lt;/td&gt;
    &lt;td bgcolor="&lt;? echo $color;?&gt;"&gt;&lt;? echo $row['ont_id'];?&gt;&lt;/td&gt;
        &lt;td bgcolor="F4F4FF"&gt;&lt;? echo $row['hostname'];?&gt;&lt;/td&gt;
        &lt;td bgcolor="F4F4FF"&gt;&lt;? echo $row['pon_port'];?&gt;&lt;/td&gt;
        &lt;td bgcolor="F4F4FF"&gt;&lt;? echo $row['splitter'];?&gt;&lt;/td&gt;
        &lt;td bgcolor="F4F4FF"&gt;&lt;? echo $row['splitter_port'];?&gt;&lt;/td&gt;
        &lt;td bgcolor="F4F4FF"&gt;&lt;? echo $status ; ?&gt;&lt;/td&gt;
        &lt;td bgcolor="F4F4FF"&gt;&lt;? echo $match_status; ?&gt;&lt;/td&gt;
        &lt;td bgcolor="F4F4FF"&gt;&lt;? echo $ont_distance; ?&gt;&lt;/td&gt;
        &lt;td bgcolor="F4F4FF"&gt;&lt;? echo $image; ?&gt;&lt;/td&gt;

    &lt;/tr&gt;
    &lt;?
    $k++;
    }
   ?&gt;
    &lt;/table&gt;

=============================
</code></pre>

<p>Sorry, I missed the question part while posting: Actually the above scripts will output the device status list up or down. But this code is displaying all devices down if one device goes down. </p>
