/    Sign up×
Community /Pin to ProfileBookmark

Check If image overlaps another image

Hi,

I’m trying to make a script for a pixel ad site. So far I have come up with this;

On the index page:

<form action=buy.php method=post ><td colspan=3 height=1000 valign=top background=images/grid3.gif>
<input name=whereami type=image class=botborder src=images/px.gif align=top width=”1000″ height=1000 />
</form>

When you click on the image on this page it takes you to buy.php

on buy.php there is a form with the x location and y location of the image that was clicked on, heres what this looks like:

<?php
$xloc = $_POST[‘whereami_x’]; // x co-ordinate
$yloc = $_
POST[‘whereami_y’]; // y co-ordinate

//this is making the grid give blocks of 10 pixels. so first block is 0,0 next box is 1,0 and so on.
$x_loc = floor($xloc/10);
$y_loc = floor($yloc/10);

echo ‘<br />x =’.$x_loc;
echo ‘<br />y =’.$y_loc;
?>

On the form the customer can choose number of horizontal and vertical blocks for their image. Once they submit form it goes to confirm.php. Now, this is where I’m having a problem. On confirm.php I want it to check if the the number of blocks the customer chose would overlap an image of an existing customer (I have a table in mysql database that stores x and y locations, number of width blocks and height blocks of existing customers). I have absolutely no idea how to do this.
I would apreciate if anyone can give me any ideas or suggestions on this.

Many thanks in advance

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@SpectreReturnsFeb 25.2006 — I'd extract all of the blocks and who owns them from the database and put it into a 2D array, something like:
[code=php]// nobody owns it
$map[$y][$x] == null
/* OR */
// somebody owns it
$map[$y][$x] == $owner[/code]

Now, you'd need to go through every block and make sure it isn't owned, something like this:

[code=php]$map = ...; // populate the $map variable
$good = true;
$width = count($map[0]);
$height = count($map);

for ($y=0; $y<$height; $y++) {
for ($x=0; $x<$width; $x++) {
if ($map[$y][$x] != null) {
$good = false;
break 2;
}
}
}

if ($good) {
echo "The region is good!";
} else {
echo "The region countains already owned blocks. :(";
}[/code]
Copy linkTweet thisAlerts:
@dreamer84authorFeb 26.2006 — Hey SpectreReturns,

Thank you for your reply. I just have a few questions and a request. I'm not sure how to populate the $map variable - is this the x and y data from the table of existing customers in db?

Are the variables $x and $y the data of the post variables or from the database?

Here is the code from confirm.php:

[code=php]<?php

include 'pixeldb.php';
# grab the POST variables from the HTML form,

$name = $_POST['name'];
$email = $_POST['email'];
$x1 = $_POST['x1'];
$y1 = $_POST['y1'];
$wblocks = $_POST['wblocks'];
$hblocks = $_POST['hblocks'];
$url = $_POST['url'];
$message = $_POST['message'];
$amount = $_POST['amount'];


# Any escaped characters? No, not a jailbreak...something that could
# cause errors when PHP processes. We check for that here.
$name = stripslashes($name);

$email = stripslashes($email);
$x1 = stripslashes($x1);
$y1 = stripslashes($y1);

$wblocks = stripslashes($wblocks);
$hblocks = stripslashes($hblocks);
$url = stripslashes($url);
$message = stripslashes($message);

$amount = stripslashes($amount);

?>[/code]


I have a table named 'customer' in mysql db, with the fields 'x_pos', 'y_pos', 'width_blocks' and 'height_blocks' and fields for other customer details.

If you can please show me an example of the code you posted with my variables above, that would clear a lot of confusion.

Also I'm not sure how to extract all of the blocks from the database and put it into a 2D array.

Thank you
Copy linkTweet thisAlerts:
@SpectreReturnsFeb 26.2006 — The map would be the information for every block, sorted like this:
[code=php]$map[{from database}y_pos][{from database}x_pos] = {from database}customer;[/code]

I'm writing this off my head, so it might work, but I suspect something like this to populate the map:
[code=php]$map = array();
$map_width = 20; // the map is made up as a 20x20 grid
$map_height = 20; // change these to suit how big your map should be

$querytpl = "SELECT customer FROM the_table WHERE y_pos=%s"; // change the_table to the table you're working in

for ($y=0; $y<$map_height; $y++) {
$result = mysql_query(sprintf($querytpl, "$y")); // typecast so it responds to %s
$row = mysql_fetch_row($result);
foreach ($row as $x => $customer) {
$map[$y][$x] = $customer;
}
}[/code]


Here's the version of my older code so it'll work with what you said:
[code=php]include 'pixeldb.php';

$postfields = array("name", "email", "x1", "y1", "wblocks", "hblocks", "url", "amount", "message");

foreach ($postfields as $field) {
$$field = stripslashes($_POST[$field]);
}
// I just cut down all of your manual definitions for the POST variables


$map = ...; // use the code above
$good = true;

for ($y=$y1; $y<($hblocks + $y1); $y++) {
for ($x=$x1; $x<($wblocks + $x1); $x++) {
if ($map[$y][$x] != null) {
$good = false;
break 2;
}
}
}

if ($good) {
echo "The region is good!";
} else {
echo "The region countains already owned blocks. :(";
}[/code]
Copy linkTweet thisAlerts:
@dreamer84authorMar 13.2006 — Hi,

I've been trying the above code out and now I'm getting errors.

These are the errors I'm having trouble fixing:

Notice: Undefined variable: customer in C:Websitesconfirm.php on line 61

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in C:Websitesconfirm.php on line 75

Warning: Invalid argument supplied for foreach() in C:Websitesconfirm.php on line 76

Line 61:
[code=php]$map['ypos']['xpos'] = $customer;[/code]

Line 75:
[code=php]$row = mysql_fetch_row($result);[/code]

Line 76:
[code=php] foreach ($row as $x => $customer) {
$map[$y][$x] = $customer;
}
} [/code]



Here's the entire php code from confirm.php:


[code=php]<?php
include 'pixeldb.php';

$query = "SELECT * FROM transaction";
$data = mysql_query($query) or die (mysql_error()."<br />Couldn't execute query: $query");

$map = mysql_fetch_array($data);


$map['ypos']['xpos'] = $customer;


$map = array();
$map_width = 20; // the map is made up as a 20x20 grid
$map_height = 20; // change these to suit how big your map should be

$querytpl = "SELECT name FROM transaction WHERE y_pos=%s"; // change the_table to the table you're working in

for ($y=0; $y<$map_height; $y++) {
$result = mysql_query(sprintf($querytpl, "$y")); // typecast so it responds to %s
$row = mysql_fetch_row($result);
foreach ($row as $x => $customer) {
$map[$y][$x] = $customer;
}
}



$good = true;

for ($y=$y1; $y<($hblocks + $y1); $y++) {
for ($x=$x1; $x<($wblocks + $x1); $x++) {
if ($map[$y][$x] != null) {
$good = false;
break 2;
}
}
}

if ($good) {
echo "The region is good!";
} else {
echo "The region contains already owned blocks. :(";
}

?>[/code]
×

Success!

Help @dreamer84 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...