Jump to content

X Y Coordinate grid, using PHP with data in SQL


Admiral S3

Recommended Posts

Hello all:

 

Let me give you some background, over the past 10 years I have dealt with many forums, and even built some limited PHP scripts myself for various things related to those forums. Usually modifications to expand the forums abilities. So I had thought I had a fairly good handle on PHP in general... LOL, that is until recently when I decided to take on a slightly different/larger challenge.

 

Recently one of my forum users requested the creation of a X,Y Coordinate grid, where they could put into it certain bits of information and it would return showing them the X,Y location of thier various work attached in some way shape or form to his site.

 

I had thought this would be a simple project, as I have worked with GD image creation before, as well as SQL statements many times, it simply should have been a blending of my exsisting knowledge into one program. While this was a unique challenge for me, its one I seem to have been unable to plug.

 

I have searched the web extensively the last 2 weeks hoping to find a tutorial, and sadly while I can find a great many related to bar graphs and line graphs, I can't seem to find a single one related to a simple X Y coordinate graph, yet I know I have seen them done many times.

 

I am at a total loss on this one and so far my attempts have garnered me 0 results. I can't even create a 20 x 20 grid with any success... LOL.

 

I am hoping one of you would be kind enough to post a tutorial, with a simple code that would produce a grid using GD, or even some other form of image generation, with 1 - X Y plot in it generated either from SQL or from the program its self, so that I could see a working version and with luck go from there to expand it to my needs.

 

Thank you.

Link to comment
Share on other sites

Err well.... not quite what I had in mind, I would of course rather learn that which I am missing then have a program do it for me. So while I thank you for your input I will hold out a bit and see if someone has a base line code I can start from, learn and then expand from there on.

Link to comment
Share on other sites

LOL, alright perhaps I will have a touch better luck with this route.

 

Desired effect: List locations of all persons in the database, in this case enemies only.

This is an example of the code, obviously SQL connections are disabled, and as this is the test grid, the size is significantly smaller then the desired size.

 

MYSQL databse contains one entry with the following data.

lord_num, (auto inc) 1

lord_name, varchar (16), Admiral S3

alliance, varchar (40), {no data entered.}

x, decimal (5,0), 40

y, decimal (5,0), 40

type, varchar (20), Member (this is reserved for later versions of this grid, eventually this will allow users to select "show members", "show allies", "Show Enemies", "show neutrals".)

 

 

THE CODE:

<?php

 

// set the HTTP header type to PNG

header("Content-type: image/png");

 

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

 

// Image Size

$height = 618;

$width = 618;

$im = ImageCreate($width, $height);

// this is the image

 

// Colors

$grass = ImageColorAllocate($im, 67,154,67);

$enemy = ImageColorAllocate($im, 255,0,0);

$bgcolor = ImageColorAllocate($im, 39,104,39);

 

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 `mapdata`";

// 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<=100; $b++)

// the grid is 100 x 100, this will make the Y columns

{

for($i=0; $i<=100; $i++)

// the grid is 100 x 100, this will make the X rows

{

if ($userarray[$i  . ','  . $b] == true)

// If the $userarray says that there is somebody 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);

}

}

}

?>

 

 

Current Output results in a single white square. So does anyone have any idea why this code is not generating the results desired, and if so... is willing to show me where the error is and in some detail explain to me why its not working as expected. This way I can learn where the problem lies and why the problem exsists at all.

 

Thank you.

Link to comment
Share on other sites

  • 4 weeks later...

ubersmuck:

 

Thank you... thats at least very close to what I want. I can write a code string to get that to show up at the very least.

 

Daniel0:

 

What I am attempting to do, is to have PHP produce a 2D X, Y coordinate grid appear complete with data from SQL, or MySQL.

 

Basically think of it as a map, as that what it is from what I understand for a game one of my customers plays. Basically showing where his friends are, and where his enemies are.

 

PHP may not exactly be the best way to represent this, but its what I know to a fair degree. LOL However if anyone has any alternative code strings that could be of use I am always willing to expand my knowledge.

 

 

Link to comment
Share on other sites

http://simplysimonhosting.com/map.png

 

In case anyone wanted to know exactly what I am looking for. The above is so far the best that can be generated. Essentially the red dot represents a person in this game.

 

Ultimatly it would great to have some mouse over details when a person moves thier mouse over the dot to show what that persons status is, and what they are, IE friend, or foe.

Link to comment
Share on other sites

  • 2 years later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.