用PHP中的数据库给定行id和列id检查表的行和列编号


Check the table row and column number with database give row id and column id in PHP

在我的项目中,我使用两个for循环打印了固定的10*10表。我的数据库有两个字段row_idcolumn_id。我想检查数据库给定的行id和列id是否与表的行编号和列编号相同,并打印一些字符串。但在下面的代码中,只检查第一个数据库值。下面的代码有什么问题?

print('<table border="1" cellspacing="0" cellpadding="2">');
 for($x=1; $x<=10; $x++)
    {
     echo "<tr>";
    for($y=1; $y<=10; $y++)
       {
      echo "<td>";

        while($row = mysqli_fetch_array($result)){
        if($row['row_id'] == $x && $row['column_id'] == $y)
        print($row['img_title']);
        else echo"no img";
        }

      echo $x . $y;
      echo "</td>"; 
       }
     echo "</tr>";
    }
print('</table>');
while($row = mysqli_fetch_array($result)) {
   // ....
}

因为这段代码将一次获取所有行(在您的情况下,是您的第一列和第一行),并且您不能在其他列和行重用它。您可以获取所有行并将其存储在临时数组中。

例如

while($row = mysqli_fetch_array($result)) {
   $temp[$row['row_id']][$row['column_id']] = true;
}

然后在您的嵌套循环中:

for($x = 1;  $x <= 10; $x++) {
   for($y = 1; $y <= 10; $y++) {
       if (isset($temp[$x][$y])) {
           print("got");
       }
   }
}