doufeiqiong3515 2016-10-06 20:25
浏览 228
已采纳

根据for循环的输出更改div颜色

How do I implode the output of below code into divs instead of echo "On" or "Off"?

I also want the divs to have background color of green if node is ON, and background of red if node is OFF:

<html>
<body>

<?php
for ($ipa=65; $ipa <= 72; $ipa++){
   $ip = "10.32.12.".$ipa;
   $ping_output=array();
   exec ("ping -n 1 -w 1 $ip 2>&1", $ping_output, $return_val);
   //echo $ip." -> ".$return_val."<br/>".implode('<br/>',$ping_output).'<br/>';

if(stripos($ping_output[2],"TTL")!==false) {
     echo $ip." is On <br/>";
   } else if(stripos($ping_output[2],"unreachable")!==false){
     echo $ip." is unreachable <br/>";
   } else if(stripos($ping_output[2],"request")!==false){
     echo $ip." is Off <br/>";
   }
}
?>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • duandao1931 2016-10-06 21:35
    关注

    here is something that I did, hope it helps you:

    back-end, q3.php

    <?php
    $result = array();
    for ($ipa=65, $i = 1; $ipa <= 72; $ipa++, $i++){
       $ip = "10.32.12.".$ipa;
       $ping_output=array();
    
       exec ("ping -n 1 -w 1 $ip 2>&1", $ping_output, $return_val);
       //echo $ip." -> ".$return_val."<br/>".implode('<br/>',$ping_output).'<br/>';
    
    if(stripos($ping_output[2],"TTL")!==false) {
    //     echo $ip." is On <br/>";
    
         $result["enode".$i] = "on";
       } else if(stripos($ping_output[2],"unreachable")!==false){
    //     echo $ip." is unreachable <br/>";
           $result["enode".$i] = "unreachable";
       } else if(stripos($ping_output[2],"request")!==false){
    //     echo $ip." is Off <br/>";
           $result["enode".$i] = "off";
       }
    
    }
    echo json_encode($result);
    ?>
    

    front-end, q3h.html

    <!DOCTYPE html>
    <html>
    
        <head>
            <title>Node Status</title>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
            <style>
                #wrap {
                    width: 100%;
                    margin: auto;
                }
    
                div[id*="enode"] {
                    display: inline-block;
                    margin: 10px;
                    width: 100px;
                    height: 100px;
                    background-color: green;
                    text-align: center;
                }
            </style>
        </head>
        <body>
            <div id="wrap">
                <div id="enode1">Node1</div>
                <div id="enode2">Node2</div>
                <div id="enode3">Node3</div>
                <div id="enode4">Node4</div>
                <div id="enode5">Node5</div>
                <div id="enode6">Node6</div>
                <div id="enode7">Node7</div>
                <button>update nodes</button>
            </div>
            <script>
                $(document).ready(function () {
    
    
                    $("button").click(function () {
                        $.ajax({
                            url: "q3.php",
                            dataType: 'json',
                            success: function (result) {
                                alert(result);
                                for (key in result) {
                                    id = "#"+key;
                                    if (result[key] == "on") {
                                        $(id).css("background-color", "green");
                                    }
    
                                    if (result[key] == "unreachable") {
                                        $(id).css("background-color", "yellow");
                                    }
    
                                    if (result[key] == "off") {
                                        $(id).css("background-color", "red");
                                    }
                                }
    
    
                            }});
                    });
                });
    
    
    
            </script>
        </body>
    </html>
    

    so basically, this is what you should do, don't be shy and tell me if this works for you. :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 请详细说明问题背景 11月12日

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?