Hi, I have a site here: ukadulthomepage(dot)co.uk/index.php?verify=true Which functions fine apart from when i add a pixel i can add another one over the top of this which should not happen. Here is the code for the site for when a block is chosen, but i need it to check some where in here that it is not covering an existing block. <?php $posx = $_GET[x]; $posy = $_GET[y]; $startx = 0; $endx = 0; $starty = 0; $endy = 0; if ($_GET[ox] > $posx) { $startx = $posx; $endx = $_GET[ox]; } else { $startx = $_GET[ox]; $endx = $posx; } if ($_GET[oy] > $posy) { $starty = $posy; $endy = $_GET[oy]; } else { $starty = $_GET[oy]; $endy = $posy; } $width = ((($endx+1) - $startx)*10); $height = ((($endy+1) - $starty)*10); $totblocks = (($endx+1) - $startx) * (($endy+1) - $starty); $price = $totblocks * 4; ?> Code (markup): Thank you in advance for you help. Adam
Well, assuming you are storing startx, endx, starty and endy in a database for each ad block you've already sold, then you can easily check if you're overlapping anything. Logically, as a block has 4 corners, you need to check if each corner is within any other existing block. If any corners are, then you're overlapping. sx,sy+------------+ex,ey | | | | +------------+ sx,ey ex,ey Code (markup): So... SELECT * FROM <YourTableName> WHERE ('$startx' >= startx AND '$startx' <= endx AND '$starty' >= starty AND '$starty' <=endy) OR ('$endx' >= startx AND '$endx' <= endx AND '$starty' >= starty AND '$starty' <=endy) OR ('$startx' >= startx AND '$startx' <= endx AND '$endy' >= starty AND '$endy' <=endy) OR ('$endx' >= startx AND '$endx' <= endx AND '$starty' >= starty AND '$starty' <=endy) LIMIT 1 Code (markup): ....should check the top left, top right, bottom left and bottom right corners in turn. If that query returns a record, you've got an overlap. If it doesn't you don't. I THINK!
Sorry i should have mentioned how the existing entries are stored. I am using an XML database. And is stored like this: <?xml version="1.0" encoding="UTF-8"?> <news> <item> <id>1</id> <user>1185868374</user> <posx>16</posx> <posy>8</posy> <image>1.noimage.JPG</image> <link>http://www.test.com</link> <alt>Test</alt> <w>10</w> <h>10</h> </item> </news> Code (markup): Cheers
Well, the solution is the same, but you'll probably need to read all the xml'd entries into an array and then iterate through them comparing each entry in much the same way as you would with the database select. It'll just be a 4 part if rather than a 4 part where clause. Your complicating it a bit by using start and end in one place and start and length in another, but it's easy to convert between the two. if (('$startx' >= $array[$row]['startx'] AND '$startx' <= $array[$row]['endx'] AND '$starty' >= $array[$row]['starty'] AND '$starty' <=$array[$row]['endy']) OR ('$endx' >= $array[$row]['startx'] AND '$endx' <= $array[$row]['endx'] AND '$starty' >= $array[$row]['starty'] AND '$starty' <=$array[$row]['endy']) : : etc... ){ //then it's overlapping }
Thanks for your reply Nick.. I am a little confused as i have not dealt with XML before and i did not write this so am struggling a little. <?php $posx = $_GET[x]; $posy = $_GET[y]; $startx = 0; $endx = 0; $starty = 0; $endy = 0; if ($_GET[ox] > $posx) { $startx = $posx; $endx = $_GET[ox]; } else { $startx = $_GET[ox]; $endx = $posx; } if ($_GET[oy] > $posy) { $starty = $posy; $endy = $_GET[oy]; } else { $starty = $_GET[oy]; $endy = $posy; } $width = ((($endx+1) - $startx)*10); $height = ((($endy+1) - $starty)*10); $totblocks = (($endx+1) - $startx) * (($endy+1) - $starty); $ch = file('./db.xml'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $value = curl_exec($ch); preg_match_all('/<(posx|posy|w|h)>([^<]+)<\/\\1>/', $value, $matches); if (('$startx' >= $array[$row]['startx'] AND '$startx' <= $array[$row]['endx'] AND '$starty' >= $array[$row]['starty'] AND '$starty' <=$array[$row]['endy']) OR ('$endx' >= $array[$row]['startx'] AND '$endx' <= $array[$row]['endx'] AND '$starty' >= $array[$row]['starty'] AND '$starty' <=$array[$row]['endy'])){ echo "You cannot place this above another image"; } $price = $totblocks * 4; ?> Code (markup): This is what i have got below but i think it is completey wrong :S Please can you point me in the right direction. Cheers, Adam
Yep - completely wrong! Sorry! You've taken my example a little literally. I think you need to do a lot more to organise the xml file once you read it in before you start looping through it. And you're not currently looping through it, and your if condition is only half what you need (as I only explicitly typed half of it). I can make this work for you (probably) Question now is which would you rather... 1) I take you through this step by step, which will force you to learn what the code is doing and force you to think how to solve it? 2) I give you an easy way out which means I do all the work, and you get the glory for no effort? Think carefully!
See the problem is although i want to learn i simple don't have the time and my expertise do not lie within coding but within other areas. I simply just pick up programming when i need to and not as my main role. Although i would love to learn it properly i don't have the time in my life to do so. That being said as much as i am embarrassed to say this please can i take the second option. Thanks a lot and sorry to be a pain.