0
SQL結構從數據庫中抽取數據自動更新PHP循環
CREATE TABLE IF NOT EXISTS `map` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`x` int(11) NOT NULL,
`y` int(11) NOT NULL,
`type` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
http://localhost/map.php?x=0&y=0
當我更新通過POST x和y或GET,我想拉離新數據數據庫沒有刷新網站,我將如何管理?有人能給我一些例子,因爲我真的被困在這裏。
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('hol');
$startX = $_GET['x'];
$startY = $_GET['y'];
$fieldHeight = 6;
$fieldWidth = 6;
$sql = "SELECT id, x, y, type FROM map WHERE x BETWEEN ".$startX." AND ".($startX+$fieldWidth). " AND y BETWEEN ".$startY." AND ".($startY+$fieldHeight);
$result = mysql_query($sql);
$positions = array();
while ($row = mysql_fetch_assoc($result)) {
$positions[$row['x']][$row['y']] = $row;
}
echo "<table>";
for($y=$startY; $y<$startY+$fieldHeight; $y++) {
echo "<tr>";
for($x=$startX; $x<$startX+$fieldWidth; $x++) {
echo "<td>";
if(isset($positions[$x][$y])) {
echo $positions[$x][$y]['type'];
}
else {
echo "(".$x.",".$y.")";
}
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
使用AJAX。它會允許你這樣做 – hjpotter92 2012-04-03 17:25:42
我是否需要改變輸出,或者我可以保留我的php表代碼? – 2012-04-03 17:28:59
只需注意:我不會「回顯」輸出的每一行。只需創建一個變量並不斷更新,然後在代碼結尾處回顯(或返回)該變量。 – jwhat 2012-04-03 17:30:13