如何在PHP中应用循环到容器

如何在PHP中应用循环到容器,php,html,mysql,Php,Html,Mysql,我正在尝试使用MySQL在WAMP服务器中运行PHP代码。我正在创建一个容器, 我不知道我需要多少容器。不知怎的,我有了动态生成的容器数 我要尽可能多的容器。我想根据容器的数量将for循环应用于下面的代码 我还需要基于计数创建CSS容器类 在我使用where子句where Entity='ATA'的查询中,我在where子句中应用了数据数组,这里我只显示了一个ATA,我需要基于计数的容器 <form> <div id="container"> <div

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

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

我还需要基于计数创建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> 

比方说,您希望在where子句中指定的所有标准都存储在数组$entityArr中。然后,您应该在元素顶部添加一个额外的循环。请尝试以下代码


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

为什么要覆盖$rowsA[$i]?你在哪里加了$i?并且id在HTML中应该是唯一的。是的,我知道id应该是唯一的,但我需要基于计数动态地使用不同的容器,计数不是固定的,我如何才能做到这一点在查询中我正在应用where子句where entity='ATA',在where条件下我有不同数量的数据,基于所有数据,我需要对所有不同的数据执行查询和contianers,当我运行for循环时,会出现以下错误:-未定义变量:我们已更新了答案。只需添加$rowsA=array;在foreach和unset$rowsA之前;如代码示例中所示。让我知道它是否有效。PS:目前无法在我的计算机上运行php,所以我无法测试它。
<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 -->