Automatically update php loop with data pulled from database

Posted by John Svensson on Stack Overflow See other posts from Stack Overflow or by John Svensson
Published on 2012-04-03T17:21:55Z Indexed on 2012/04/03 17:30 UTC
Read the original article Hit count: 160

Filed under:

SQL STRUCTURE

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

When I update the x and y via POST or GET, I would like to pull the new data from the database without refreshing the site, how would I manage that? Could someone give me some examples, because I am really stuck here.

<?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>"; 

?>

© Stack Overflow or respective owner

Related posts about php