Originally posted by Erestar
As you seem to be aware, things like what you're doing where there's a lot of copy and pasted code with a few values changed can almost always be written with a loop that change some values with each pass.
As an example, though; here's the first part. I've replaced $blockn with $block[n. Similarly for $playern.
for($i=-2; $i<=2; $i++)
{
for($j=-2; $j<=2; $j++)
{
$block[12+$j-$i*5] = mysql_fetch_array(mysql_query("SELECT x,y,num,type
FROM world
WHERE x=($userinfo[x]+$j) AND y=($userinfo[y]+$i)"));
$player[13+$j-$i*5] = mysql_fetch_array(mysql_query("SELECT *
FROM players
WHERE x='".$block[13+$j-$i*5]['x']."' AND y='".$block[13+$j-$i*5]['y']."'"));
}
}
But you really shouldn't be needing to make anywhere near so many database queries. Wouldn't "(x>=$userinfo[x]-2 AND x<=$userinfo[x]+2) AND (y>=$userinfo[y]-2 AND y<=$userinfo[y]+2)" net the same blocks data in one query instead of a couple of dozen? (Untested code follows. Of course, the code above is untested as well....)
$results = mysql_query("SELECT
w.x as x,w.y as y,w.num as wnum, w.type as type,
p.num as pnum, p.dir as dir, p.username as username
FROM
world w,
players.p
WHERE
(x>=$userinfo[x]-2 AND x<=$userinfo[x]+2)
AND
(y>=$userinfo[y]-2 AND y<=$userinfo[y]+2)
AND p.x=w.x
AND p.y=w.y");
foreach($results as $result)
{ $block[$result['x']][$result['y']] = array(
'num'=>$result['wnum'],
'type'=>$result['type']);
$player[$result['x']][$result['y']] = array(
'num'=>$result['pnum'],
'dir'=>$result['dir'],
'username'=>$result['username']);
}
In short, use a two-dimensional block[][] array to represent your two-dimensional block data.
As Erestar implied, if you find yourself doing the same thing over and over, consider getting the computer to do it for you instead. That after all is what it's good at!