Question about MySQL

He has: 296 posts

Joined: May 2002

Is there a way to select aan item from a MySQL db if it's 5 number ints off?

EX. I'm at x=5 y=10 and a location on the same plane is at x=9 y=14. How would I get that row even though it's not equal to it? (You: 5,10 Location: 9,14, I want it to think that 'you' are at 'location' even though you aren't.)

if I confused you, I'm really sorry! I don't exactly know how to explain it :-\

I know how to do it, but it would end up being 5000k, don't want that just for something like this...

[James Logsdon]

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I'm corn-fuzed.

Do you want to always add 5 to the x,y coordinate? I'm guessing not, as that shouldn't require a 5MB file.

What does mySQL have to do with this problem?
(what do your tables look like)

How do you identify which 'location' to set 'you' to?
(are there any rules/formulas to follow)

Mark Hensler
If there is no answer on Google, then there is no question.

He has: 296 posts

Joined: May 2002

I think this is how I would do it:

<?php
$query
= $DB_site->query(\"SELECT * FROM rpg_locations ORDER by id DESC\");
while(
$mapr = $DB_site->fetch_array($query)) {
 
$mx = $mapr[x];
 
$my = $mapr[y];
  while(
$x != (($mx - 5) || ($mx - 4) || ($mx - 3) || ($mx - 2) || ($mx - 1) || ($mx) || ($mx + 1) || ($mx + 2) || ($mx + 3) || ($mx + 4) || ($mx + 5))) {
    while(
$y != (($my - 5) || ($my - 4) || ($my - 3) || ($my - 2) || ($my - 1) || ($my) || ($my + 1) || ($my + 2) || ($my + 3) || ($my + 4) || ($my + 5))) {
     
$y++;
    }
   
$x++;
    }
  }
}
?>

But I was hoping I could condense it some way...

[James Logsdon]

He has: 1,016 posts

Joined: May 2002

necrotic, I'm not sure if I understand you correctly or not, but do you want to select MySQL rows that are 5 more or less than a given number? If so...

SELECT * FROM rpg_locations WHERE column <= $nr + 5 OR column >= $nr - 5 ORDER BY id DESC
'

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Should be an OR, not an AND in the WHERE clause.
I don't know of any numbers that are both 5 above and 5 below a given constant. Wink

He has: 296 posts

Joined: May 2002

I brainstormed for about 1.5 hours tonight on how to do this. I'll post it tomorrow because I'm going to bed tonight. But I think it will work.

He has: 296 posts

Joined: May 2002

Ok, this is the code I came up with:

<?php
$query
= $DB_site->query(\"SELECT * FROM rpg_locations ORDER by id DESC\");
$maxr = $DB_site->num_rows($query);

for(
$coord[xmax]='1000'; $x+5 > $coord[xmax]; $coord[xmax]--) {
  for(
$coord[xmin]='0'; $x-5 > $coord[xmin]; $coord[xmin]++) {
    for(
$coord[ymax]='1000'; $y+5 > $coord[ymax]; $coord[ymax]--) {
      for(
$coord[ymin]='0'; $y-5 > $coord[ymin]; $coord[ymin]++) {
       
$query=$DB_site->query(\"SELECT * FROM rpg_locations WHERE xmax='$coord[xmax]' AND xmin='$coord[xmin]' AND ymax='$coord[ymax]' AND ymin='$coord[ymin]'\");
       
$result=$DB_site->fetch_array($query);
        if (
$result) {
         
$lid = $result[id];
         
$location = $result[name];
        } else {
         
$lid = '0';
         
$location = 'Wilderness';
        }
       
$DB_site->query(\"UPDATE user SET location='$lid'\");
        Location();
      }
    }
  }
}

function Location() {
....
}
?>

I'll be testing it out in a few minutes.

[James Logsdon]

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

<?php
$query
= \"SELECT *\"
        .\" FROM rpg_locations\"
        .\" WHERE ( xmax > (
$x+5)\"
        .\"         OR xmin < (
$x-5) )\"
        .\"     AND ( ymax > (
$y+5)\"
        .\"         OR ymin < (
$y-5) )\"
        .\" ORDER BY id DESC\";

$result=$DB_site->query($query);
while (
$result && $row=$DB_site->fetch_array($result)) {
    if (
$row[id]!=\"\") {
       
$lid = $row[id];
       
$location = $row[name];
    }
    else {
       
$lid = '0';
       
$location = 'Wilderness';
    }
   
$DB_site->query(\"UPDATE user SET location='$lid'\");
    Location();
}
?>
That will change the location once for each record found. And all records in the `user` will be affected.

Mark Hensler
If there is no answer on Google, then there is no question.

He has: 296 posts

Joined: May 2002

Mark, I tried yours. It's good, but I had to change something:

<?php
   
if($result) {
     
$row=$DB_site->fetch_array($result);
      if (
$row[id]!=\"\") {
       
$lid = $row[id];
       
$location = $row[name];
      }
    } else {
     
$lid = '0';
     
$location = 'Wilderness';
    }
?>

the While() statement wasn't working

[James Logsdon]

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.