如何使用PHP在X,Y坐标处创建带有WHILE和/或FOREACH循环的DIV-box?
以下是我试图实现的目标:如何使用PHP在X,Y坐标处创建带有WHILE和/或FOREACH循环的DIV-box?,php,html,css,mysqli,Php,Html,Css,Mysqli,以下是我试图实现的目标: 将每个div/box定位到另一个div中相应的X、Y坐标此处的帮助PLZ 我希望将每个分区/方框定位到“map_size”分区中的相应坐标。我所有的方框都只是以直线相互放置。提前谢谢。我只想使用HTML、PHP和CSS 我所拥有的: <?php include 'db_conn.php'; //query to get X,Y coordinates from DB $coord_sql = "SELECT x_coord, y_coord FROM coo
- 将每个div/box定位到另一个div中相应的X、Y坐标此处的帮助PLZ
<?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());
}
?>
我想要什么(不是地图上的所有四个区域,只有一个):
我的php位于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());
}
?>
不是100%清楚你想要什么,但这就是你想要的吗
<?php
//get number of rows for X,Y coords in the table
while($row = mysqli_fetch_assoc($coord_result)){
//naming X,Y values
$desk[] = array('x' => $row['x_coord'],
'y' => $row['y_coord']);
foreach($coord_result as $row){
echo"<div id='desk_box' style='top:".$desk[0]."; left:".$desk[1]."';">box number #number written here</div>";
}
//draw a box with a DIV at its X,Y coord
} //end while coord_result loop
?>
那么这个呢:
<div id="map_size" align="center">
<?php
//get number of rows for X,Y coords in the table
while($row = mysqli_fetch_assoc($coord_result)){
//naming X,Y values
$desk[] = array('x' => $row['x_coord'],'y' => $row['y_coord']);
foreach($coord_result as $row){
echo '<div style="postion:absolute;left:'. $row['x_coord'] . 'px; top:'. $row['y_coord']'.;" id=desk_box>box number #number written here</div>';
}
//draw a box with a DIV at its X,Y coord
} //end while coord_result loop
?>
</div>
没有位置:中间。确切位置在哪里?我刚刚更新了我的代码#map#u size有位置:center基本上我在做一个笛卡尔地图,希望将每个框放置在适当的位置上。。另外,我刚刚测试了你的代码,语法是错误的。。我不得不将双引号改为单引号。。当我的表中只有5行时,循环并没有停止,它继续运行。我不得不将for循环放在while循环之外。我得到了5个框,这是好的,现在我想把他们在适当的位置我添加了必要的CSS和固定的引号。希望这能奏效。谢谢!我使用了position:relative,它起作用了。我保持了php代码的原样。你知道我怎样才能在这些盒子之间留出空间,这样它们就不会彼此之间有空隙了吗@库珀西泰不明白这个问题。“彼此之间的差距”是什么意思?如果它们彼此靠近,绝对位置将重叠。我必须将foreach循环放置在while之外才能工作。我们可以在哪里看到它,或者你可以粘贴一些生成的html?我让它工作得很好。现在我只想使用css,每次在屏幕上绘制这些框和div时,在它们之间留出空间。
#map_size{
width:800px;
height:800px;
background:#0099FF;
border-style: solid;
border-color: black;
position: relative;
}
#desk_box{
width: 20px;
height: 30px;
border:5px solid black;
margin: 10px;
position: absolute;
}
<div id="map_size" align="center">
<?php
//get number of rows for X,Y coords in the table
while($row = mysqli_fetch_assoc($coord_result)){
//naming X,Y values
$desk[] = array('x' => $row['x_coord'],'y' => $row['y_coord']);
foreach($coord_result as $row){
echo '<div style="postion:absolute;left:'. $row['x_coord'] . 'px; top:'. $row['y_coord']'.;" id=desk_box>box number #number written here</div>';
}
//draw a box with a DIV at its X,Y coord
} //end while coord_result loop
?>
</div>