【发布时间】:2009-04-10 11:59:18
【问题描述】:
以下是我用来将地图数组“翻译”为 SQL 代码的一些代码,以便在更新游戏地图时轻松更新数据库。如您所见,它将 SQL 代码打印到屏幕上,因此我可以复制和粘贴它。
随着我的地图变得越来越大,这将变得低效,因为它会由于大量输出而导致浏览器崩溃,所以我想知道是否可以让它创建一个 .txt 文件并将所有数据写入其中打印到屏幕上?
<?php
if (isset($_POST['code'])){
$map = $_POST['code'];
$map = preg_replace("/,\\s*}/i", "}", $map);
$map = str_replace("{", "[", $map);
$map = str_replace("}", "]", $map);
$map = json_decode('[' . $map . ']');
$arrayCount1 = 0;
$arrayCount2 = -1;
$H = sprintf('%05d', 00000);
$V = sprintf('%05d', 00000);
$id = 1;
echo "INSERT INTO `map` (`id`, `horizontal`, `verticle`, `image`) VALUES" . "<br />";
for ($count1 = 0; $count1 < sizeof($map[0]); $count1++){
$arrayCount2++;
$arrayCount1 = 0;
$V = sprintf('%05d', $V + 1);
$H = sprintf('%05d', 00000);
for ($count2 = 0; $count2 < sizeof($map); $count2++){
echo "(" . $id . ", '" . $H . "', '" . $V . "', '" . $map[$arrayCount1][$arrayCount2] . "')," . "<br />";
$arrayCount1++;
$id++;
$H = sprintf('%05d', $H + 1);
}
}
}
?>
【问题讨论】: