如何在PHP中申请循环到容器

时间:2015-01-08 10:02:28

标签: php html mysql

我正在尝试使用MySQL在WAMP服务器中运行PHP代码。我正在创建一个容器, 我不知道我需要多少个容器。不知怎的,我有动态生成的容器数。

我想要尽可能多的容器。我想根据容器的数量将for循环应用于下面的代码。

另外,我需要根据count创建CSS容器类。

在我使用where子句的查询中(Where Entity =' ATA'),我有要在where子句中应用的数据数组,这里我只显示了一个(ATA),我需要基于计数的容器。

<form>
<div id="container"> 

    <div id="content">
    <!-- all you need with Tablecloth is a regular, well formed table. No need for id's, class names... --> 
    <table  cellspacing="0" cellpadding="0">
            <tr >
                <th class="lightcolor" style="width:60px; height:30px;"><h2>Server Name</h2></th>
                <th class="lightcolor" style="width:60px; height:30px;"><h2>IP Address</h2></th>

            </tr>
<?php       
            $user_name = "root";
            $password = "";
            $database = "atssautomationgnoc";
            $server = "localhost";
            $con = mysqli_connect($server, $user_name, $password);
            $db_found = mysqli_select_db($con,$database);
            $query = "SELECT Servername,IPAddress FROM t_applicationstatus Where Entity='ATA'  order by FIELD(LiveStatus,'RTO','OK')";
            $result=mysqli_query($con,$query);
            $Names = array();
            if($result == FALSE)
            {
                die(mysql_error());
            }
            $i=0;


           while ($row = mysqli_fetch_array($result))
           {
              $rowsA[] = $row;
           }


            foreach($rowsA as $row)
            {
              $rowsA[$i]=$row['Servername'];
               $rowsA[$i] = $row['IPAddress'];

               echo "<tr>";
               echo "<td id=health>" ;
               echo $row['Servername'] ;
               echo " </td>";
              echo "<td  id=health>" ;
              echo $row['IPAddress'];
              echo " </td>";
              echo " </td>";
              echo "</tr>";
            }
?>  
    </table>    
       </div>   
    </form> 

1 个答案:

答案 0 :(得分:0)

比如说,你想在where子句中指定的所有标准都存储在数组$entityArr中。

那么你应该在<table>之上添加一个额外的循环元件。请尝试以下代码。

<div id="container"> 
    <div id="content">
<?php

$user_name = "root";
$password = "";
$database = "atssautomationgnoc";
$server = "localhost";
$con = mysqli_connect($server, $user_name, $password);
$db_found = mysqli_select_db($con,$database);

$entityArr = array("ATA", "ANOTHER", "AND_ANOTHER"); 
$rowsA = array(); //define rowsA
foreach ($entityArr as $entity)
{
?>
    <table  cellspacing="0" cellpadding="0">
            <tr>
                <th class="lightcolor" style="width:60px; height:30px;"><h2>Server Name</h2></th>
                <th class="lightcolor" style="width:60px; height:30px;"><h2>IP Address</h2></th>
            </tr>
<?php       
            //use $entity below in where condition
            $query = "SELECT Servername,IPAddress FROM t_applicationstatus Where Entity='".$entity."'  order by FIELD(LiveStatus,'RTO','OK')";
            $result=mysqli_query($con,$query);
            $Names = array();
            if($result == FALSE)
            {
                die(mysql_error());
            }

           //clear array
           unset($rowsA);
           while ($row = mysqli_fetch_array($result))
           {
              $rowsA[] = $row;
           }


            foreach($rowsA as $row)
            {
              $rowsA[$i]=$row['Servername']; //You'd better comment this line out
              $rowsA[$i] = $row['IPAddress']; //And this one

              echo "<tr>";
              echo "<td>" ;
              echo $row['Servername'] ;
              echo " </td>";
              echo "<td>" ;
              echo $row['IPAddress'];
              echo " </td>";
              echo " </td>";//This is extra, simply remove this line 
              echo "</tr>";
            }
?>  
    </table>    
<?php
}
?>
    </div> <!-- close #content -->
</div> <!-- close #container -->

有多种方法可以做到这一点,尤其是在循环中不执行sql语句的情况下。希望有所帮助。