我正在使用pp3Diso来构建游戏地图。该库使用字符串来确定要在每个网格单元格中使用的图像,如下所示:01,01,01,01,01,01,01:02,01,01,01,01,01,01:01,01,01,01,01,01,01:01,01,01,01,01,01,01:01,01,01,01,01,01,01:01,01,01,01,01,01,01
。
我试图在PHP中使用while循环拼凑这种类型的字符串,但代码似乎在第一次迭代后停止(01,01,01,01,01,01,01,01:
)。如何获取下面的代码来创建完整的字符串?
$mapStr = '';
$x = 1;
$y = 1;
while($x <= 500){
while($y <= 500){
$sql = 'SELECT * FROM '.CITIES.' WHERE city_x = '.$x.' AND city_y = '.$y;
$result = $db->sql_query($sql);
$city = $db->sql_fetchrow($result);
if($city){
$mapStr .= '02'.(($x < 500) ? ',' : '');
}else{
$mapStr .= '01'.(($x < 500) ? ',' : '');
}
$y++;
}
if($x == 500){
$mapStr .= ':';
}else if($x == 500 && $y == 500){
$mapStr .= '';
}
$x++;
}
$return['map'] = $mapStr;
此外,是否有更有效的方法来执行此操作,而不是查询每个单元格的数据库?
答案 0 :(得分:0)
在for
循环中更容易实现:
$columns = 501;
$rows = 501;
for($x = 1; $x < $columns; $x++){
for($y = 1; $y < $rows; $y++){
$sql = 'SELECT * FROM '.CITIES.' WHERE city_x = '.$x.' AND city_y = '.$y;
$result = $db->sql_query($sql);
$city = $db->sql_fetchrow($result);
if($city){
$mapStr .= '02';
}else{
$mapStr .= '01';
}
if($y < 500){
$mapStr .= ',';
}elseif($y == 500){
$mapStr .= ':<br />';
}
}
$sql = 'SELECT * FROM '.CITIES.' WHERE city_x = '.$x.' AND city_y = '.$y;
$result = $db->sql_query($sql);
$city = $db->sql_fetchrow($result);
if($city){
$mapStr .= '02';
}else{
$mapStr .= '01';
}
if($y < 500){
$mapStr .= ',';
}elseif($y == 500){
$mapStr .= ':<br />';
}
}
echo $mapStr;