2012-04-03 112 views
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>"; 

?> 
+0

使用AJAX。它会允许你这样做 – hjpotter92 2012-04-03 17:25:42

+0

我是否需要改变输出,或者我可以保留我的php表代码? – 2012-04-03 17:28:59

+0

只需注意:我不会“回显”输出的每一行。只需创建一个变量并不断更新,然后在代码结尾处回显(或返回)该变量。 – jwhat 2012-04-03 17:30:13

回答

0

您需要使用AJAX更新页面上的内容而不刷新页面。我建议使用JavaScript库jQuery

用法示例:

$.ajax({ 
    url: 'path/to/file/file.php', 
    success: function(data) { 
    $('.content-to-update').html(data); 
    } 
});