我正在尝试通过从 MySQL 读取其 X、Y 坐标来创建一个 DIV 框。我在同一个文件上使用 PHP 和 HTML。我也包含了我的 CSS(之后我会制作一个单独的 CSS 文件)。
现在我正在获取结果,但只有一个框在另一个框下。我正在做笛卡尔 map ,必须将每个 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 的左/上 Angular 绝对定位 DIV。
关于php - 如何在 PHP 中使用 while 循环创建 DIV 框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26146745/