繁体   English   中英

如何在PHP中使用while循环创建DIV框?

[英]How can I create DIV boxes with a while loop in PHP?

我试图通过从MySQL读取其X,Y坐标来创建DIV框。 我在同一文件上使用PHP和HTML。 我也包括了我的CSS(之后我将创建一个单独的CSS文件)。

目前,我正在获得结果,但是只在一个框下面一个框。 我正在做笛卡尔地图,必须将每个BOX放在适当的位置。 我想避免使用Javascript和Canvas,仅使用纯CSS,html和php,出于特定原因,我将DIV用于在其后放入信息。 下面是我的代码,在此先感谢您的帮助!

我拥有的文件顶部:

<!DOCTYPE html>
<?php
include 'db_conn.php';

//query to get X,Y coordinates from DB
$coord_sql = "SELECT x_coord, y_coord FROM coordinates";
$coord_result = mysqli_query($conn,$coord_sql);

//see if query is good
if($coord_result === false) {
    die(mysqli_error()); 
}
?>

我的CSS在头:

<style type="text/css">
    #desk_box{ width: 20px; height: 30px; border:10px solid black; margin: 10px;}   
</style>

我正在尝试遍历这里,为存在的每一行在其适当的位置创建一个div:

<div class="section_a" >
  <p>Section A</p>
  <?php

//get number of rows for X,Y coords in the table
while($row = mysqli_fetch_assoc($coord_result)){    
  //naming X,Y values
    $x_pos = $row['x_coord'];
          $y_pos = $row['y_coord'];     

    //draw a box with a DIV at its X,Y coord    
    echo "<div id='desk_box'>box here</div>";                                           
    } //end while coord_result loop
?>

</div> <!-- end div section_a -->

不,您实际在哪里将坐标分配给DIV。

像这样:

//get number of rows for X,Y coords in the table
while($row = mysqli_fetch_assoc($coord_result)){    
    //naming X,Y values
    $x_pos = $row['x_coord'];
    $y_pos = $row['y_coord'];     

    //draw a box with a DIV at its X,Y coord    
    echo "<div id='desk_box' style='position: absolute;
                                        left: " . $x_pos . "px;
                                         top: " . $y_pos . "px;'>
           box here</div>";                                           
} //end while coord_result loop

该代码将获取每个X / Y坐标,并将DIV从父DIV的左/上角绝对定位,并在每个循环中生成坐标。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM