x,y map help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
liamallan
Forum Newbie
Posts: 19
Joined: Sat Aug 14, 2010 6:25 pm

x,y map help

Post by liamallan »

hey guys, im trying to make a game map, which will be like a x,y grid, a query will get the information stored in the 'x' and 'y' columns of the database table and project them onto the map.

im having a small problem though, the image created by php doesnt show(like the image url isnt right), i think it may be because of the header reference at start of code.

here is the code:

Code: Select all

<?php
header("Content-type: image/gif"); // set as png

mysql_connect("localhost", "username", "password") or die("Mysql Error");
mysql_select_db("table") or die("Database error.");

// Image Size
$height = 247;
$width = 247;

// Image Pointer, turn on antialiasing 
$im = ImageCreate($width, $height); // this is the image
ImageAntiAlias($im, true); // turn it on, just for fun

// Colors
$grass = ImageColorAllocate($im, 67,154,67);
$enemy = ImageColorAllocate($im, 255,0,0);
$bgcolor = ImageColorAllocate($im, 39,104,39);

// exmaple color, not relevant to program.
$white = ImageColorAllocate($im, 255, 0, 0);

ImageFill($im, 0, 0, $bgcolor); // Fill the image with the background color

$size = 5; // size of a block, 5 x 5
$size2 = $size + 1; // don't change this, it is for spacing  


$query = "SELECT * FROM `users`"; // get all user information
$result = mysql_query($query);
$userarray; // initialize variable

while($row = mysql_fetch_array($result))
{
	$userarray[$row['x'] . ',' . $row['y']] = true; // load all the user info we need into an array
}

for($b=0; $b<=40; $b++) // the grid is 40 x 40, this will make the Y columns
{
	
	for($i=0; $i<=40; $i++) // the grid is 40 x 40, this will make the X rows
	{
		if ($userarray[$i . ',' . $b] == true) // If the $userarray says that there is sombody in this location
		{

			ImageFilledRectangle($im, 1+$i*$size2, 1+$b*$size2, $size+$i*$size2, $size+$b*$size2, $enemy);

		}
		else
		{
			// Nobody lives there, draw some grass.
			ImageFilledRectangle($im, 1+$i*$size2, 1+$b*$size2, $size+$i*$size2, $size+$b*$size2, $grass);
		}
	}
}  
?>
can anyone help, i been working to get this for a while now.
thanks
:banghead:
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: x,y map help

Post by Weirdan »

one obvious thing is that you're not outputting any image data. You should use imagegif() call at the end of your script (or any of image<format>() functions, just make sure it match the content type you're declaring at the top of your script).
liamallan
Forum Newbie
Posts: 19
Joined: Sat Aug 14, 2010 6:25 pm

Re: x,y map help

Post by liamallan »

hey, thanx for ur reply. i tried what u said but with no effect. there is an image trying to show (with or without the call), but its just a box with a cross in it, like image isnt available
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: x,y map help

Post by requinix »

Comment out the header() and look for error messages.
liamallan
Forum Newbie
Posts: 19
Joined: Sat Aug 14, 2010 6:25 pm

Re: x,y map help

Post by liamallan »

nope, no errors but the unavailable image sign went away, its the header that has the image values i think
liamallan
Forum Newbie
Posts: 19
Joined: Sat Aug 14, 2010 6:25 pm

Re: x,y map help

Post by liamallan »

also, i removed the header to see if there were any errors, and it displayed:
[text]�PNG IHDR����*�PLTEC�C�'h'�E���&IDATh���1 �Om O��<�~��:IEND�B`�[/text]
what does that mean?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: x,y map help

Post by Weirdan »

Seems like a png image data. Turn content type header back, but use a png content type: image/png
liamallan
Forum Newbie
Posts: 19
Joined: Sat Aug 14, 2010 6:25 pm

Re: x,y map help

Post by liamallan »

still not working :banghead:

all that gets displayed is :
Image

this is my code as it stands:

Code: Select all

<?php 
header("Content-type: image/png"); // set as png 

mysql_connect("localhost", "uname", "pass") or die("Mysql Error"); 
mysql_select_db("dbname") or die("Database error."); 

// Image Size 
$height = '247'; 
$width = '247'; 

// Image Pointer, turn on antialiasing  
$im = imagecreate($width, $height); // this is the image 
imageantialias($im, true); // turn it on, just for fun 

// Colors 
$grass = imagecolorallocate($im, '67','154','67'); 
$enemy = imagecolorallocate($im, '255','0','0'); 
$bgcolor = imagecolorallocate($im, '39','104','39'); 

// exmaple color, not relevant to program. 
$white = imagecolorallocate($im, '255', '0', '0'); 

imagefill($im, '0', '0', $bgcolor); // Fill the image with the background color 

$size = 5; // size of a block, 5 x 5 
$size2 = $size + 1; // don't change this, it is for spacing   


$query = "SELECT * FROM `addhit`" ;  // get all user information 
$result = mysql_query($query) or die("Mysql Error"); 
$userarray; // initialize variable 

while($row = mysql_fetch_array($result)) 
{ 
    $userarray[$row['x'] . ',' . $row['y']] = true; // load all the user info we need into an array 
} 

for($b=0; $b<=40; $b++) // the grid is 40 x 40, this will make the Y columns 
{ 
    for($i=0; $i<=40; $i++) // the grid is 40 x 40, this will make the X rows 
    { 
        if ($userarray[$i . ',' . $b] == true) // If the $userarray says that there is sombody in this location 
        { 

            imagefilledrectangle($im, 1+$i*$size2, 1+$b*$size2, $size+$i*$size2, $size+$b*$size2, $enemy); 

        } 
        else 
        { 
            // Nobody lives there, draw some grass. 
            imagefilledrectangle($im, 1+$i*$size2, 1+$b*$size2, $size+$i*$size2, $size+$b*$size2, $grass); 
        } 
    } 
}  

imagepng($im);
?>
the thing im wondering is, if $userarray has a value or a reason?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: x,y map help

Post by Weirdan »

It's not working because you're outputting html around the binary data:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<89>PNG
^Z
^@^@^@^MIHDR^@^@^@<F7>^@^@^@<F7>^B^C^@^@^@<BC><E2>*<C0>^@^@^@^LPLTEC<9A>C<FF>^@^@'h'<FF>^@^@E<F8><E1><C6>^@^@^@&IDATh<81><ED><C1>1^A^@^@^@ <F5>Om       O<A0>^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@<CE>^F<<C9>^@^A~<95><B1>:^@^@^@^@IEND<AE>B`<82>1
</body>
</html>
For image to display properly you need to strip all that html.
liamallan
Forum Newbie
Posts: 19
Joined: Sat Aug 14, 2010 6:25 pm

Re: x,y map help

Post by liamallan »

ahhhhhhhhhh!!! thank u so much!! :mrgreen: doesnt look like the 'imagefillrectangle' is working though.

is the $userarray variable being used correctly?
liamallan
Forum Newbie
Posts: 19
Joined: Sat Aug 14, 2010 6:25 pm

Re: x,y map help

Post by liamallan »

hey guys, i have decided to dump the header imagecreate idea, and opted for a simpler table-type map which works.....to a certain extent.

it only pulls one row from the mysql table and displays on map, instead of displaying all rows.

this is the section of code that displays each row on the map:

Code: Select all

<?php 

function DisplayGrid($gridx,$gridy) { 
    global $x, $y; 
     
    $sfield="SELECT * from addhit"; 
    $sfield2=mysql_query($sfield) or die(mysql_error()); 
    $sfield3=mysql_fetch_array($sfield2); 
     
   if($gridx == $sfield3['x'] && $gridy == $sfield3['y']){ 
        echo "<td  bgcolor=\"#FF0000\" align=center valign=center data-tooltip=\"sticky1\"></td>"; 
        print "<div id=\"mystickytooltip\" class=\"stickytooltip\">"; 
        print "<div style=\"padding:5px\">"; 

        print "<div id=\"sticky1\" class=\"atip\" style=\"width:200px\">"; 
        print "".$sfield3['x'].", ".$sfield3['y']."<br />"; 
        print "<img src=\"../hostile.JPG\" alt=\"\"/><br />"; 
        print "<b>Lord Name:</b> ".$sfield3['lordname']."<br />"; 
        print "<b>Alliance:</b> ".$sfield3['alliance'].""; 
         
        print "</div>"; 
        print "</div>"; 
        print "<div class=\"stickystatus\"></div>"; 
        print "</div>"; 
        }else{ 
    echo "<td background=\"map.png\" align=center valign=center></td>"; 
        } 
         
} 
?>
liamallan
Forum Newbie
Posts: 19
Joined: Sat Aug 14, 2010 6:25 pm

Re: x,y map help

Post by liamallan »

anyone :(
liamallan
Forum Newbie
Posts: 19
Joined: Sat Aug 14, 2010 6:25 pm

Re: x,y map help

Post by liamallan »

i have managed to get the different citys to show on the map but when the city info box(Sticky Tooltip script) pops up, all the citys hold the same information (coordinates, name etc..)

this is my code now:

Code: Select all

<?php 

function DisplayGrid($gridx,$gridy) { 
    global $x, $y; 
     
    $sfield="SELECT * from addhit"; 
    $sfield2=mysql_query($sfield) or die(mysql_error()); 
    while($sfield3=mysql_fetch_array($sfield2)){ 
     
  if ($gridx == $sfield3['x'] && $gridy == $sfield3['y']) { 
        echo "<td width=7  bgcolor=\"#FF0000\" align=center valign=center data-tooltip=\"sticky1\"><font size='1'></font></td>"; 
        print "<div id=\"mystickytooltip\" class=\"stickytooltip\">"; 
        print "<div style=\"padding:5px\">"; 

        print "<div id=\"sticky1\" class=\"atip\" style=\"width:200px\">"; 
        print "".$sfield3['x'].", ".$sfield3['y']."<br />"; 
        print "<img src=\"../hostile.JPG\" alt=\"\"/><br />"; 
        print "<b>Lord Name:</b> ".$sfield3['lordname']."<br />"; 
        print "<b>Alliance:</b> ".$sfield3['alliance'].""; 
         
        print "</div>"; 
        print "</div>"; 
        print "<div class=\"stickystatus\"></div>"; 
        print "</div>"; 
      } 
         
  } 
    echo "<td width=7 align=center valign=center><a href=\"index.php?xcord=$gridx&ycord=$gridy\"></a><font size=1></font></td>"; 
         
         
} 
?>
Post Reply