Jump to content

Simple latitude & longitude HELP


apw

Recommended Posts

Hello im trying to write a simple latitude and longitude script for my game.  So far i have a my users and locations tables set.  I have in the users table 2 entries called latitude and longitude and also in  locations table 2 entries called latitude and longitude.  When player finishes registering and everything is ok i placed a $new_lat and had it choose a random number between 0 and 5.  I did the same with $new_long had random choose a number between 0 and 5. Now i query the the locations table (after placing a few random latitude and longitude numbers between 0 and 5) and here is where im stumped.  I need assign the latitudes and longitudes that are in the locations table into possibly 2 variables and then run a check to see if the random numbers chosen match and if they do keep doing new random numbers until the randomly chosen numbers do NOT match any those in the locations table.  Can someone help me out with this?

Link to comment
Share on other sites

This should do it. Both lat and lon need to be in the table. This give zero rows if the random lat/lon is not in table.

 

<?php
$query = "SELECT COUNT(*) from table_name WHERE Lat_col = 'random_lat'";
$result = mysql_query($query);
$lat = mysql_fetch_row($result);

$query = "SELECT COUNT(*) from table_name WHERE Lon_col = 'random_lon'";
$result = mysql_query($query);
$lon = mysql_fetch_row($result);

if($lat[0] > 0 $$ $lon[0] > 0) you have a problem else its good to go 

?>

Link to comment
Share on other sites

After adding code to the script im getting an warning mysql_fetch_array expects parameter 1 to be resource null is given. Even changing the line to select count(*) from locations where latitude='latitude'"; and still gives same error

Link to comment
Share on other sites

No the info was already in the table i put 3 sets of latitude and longitude numbers to test then did a $new_lat=rand(0,5) and $new_long=rand(0,5) but with the posted code the with the count and lat[0] and lon[0] always returned something like 0 or 1 but how do i compare the 3 sets of latitude and longitude to the lat[0] and lon[0]?

Link to comment
Share on other sites

I created a $latquery do select only the latitude from my locations table and a $lonquery to select only the longitude from locations table.  Then did a $latrow = mysql_fetch_array($latquery) and a $lonrow to fetch the array.  I did a $new_lat and a $new_lon with random numbers from 0 to 5.  What would be the best way to compare the $latrow to $new_lat and $lonrow to $new_lon to where IF $latrow was equal to $new_lat and $lonrow was equal to $new_lon then how would i keep telling $new_lat and $new_lon to keep searching for a random number that isnt in $latrow or $lonrow?

Link to comment
Share on other sites

No the info was already in the table i put 3 sets of latitude and longitude numbers to test then did a $new_lat=rand(0,5) and $new_long=rand(0,5) but with the posted code the with the count and lat[0] and lon[0] always returned something like 0 or 1 but how do i compare the 3 sets of latitude and longitude to the lat[0] and lon[0]?

Lets do this right . Import this to the database your using:

/* Database export results for db hours*/

/* Preserve session variables */
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS;
SET FOREIGN_KEY_CHECKS=0;

/* Export data */

/* table structure for places */
CREATE TABLE `places` (
  `spot` tinyint(10) NOT NULL,
  `name` varchar(10) NOT NULL,
  `lon` tinyint(2) NOT NULL,
  `lat` tinyint(2) NOT NULL,
  PRIMARY KEY (`spot`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

/* data for table places */
insert into `places` values (1,"WITCH",1,1);
insert into `places` values (2,"MASK",2,2);
insert into `places` values (3,"TEETH",5,3);

/* Restore session variables to original values */
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;

This will set you up with a table containing three rows, each with a lon and lat different from each other.

 

Run this php file on it.

<?php
require ('./inc/DB_connect.php');  

$query = "SELECT COUNT(*) from places WHERE lat = 5 AND lon = 3";
$result = mysql_query($query);
$row = mysql_fetch_row($result);


if($row[0] == 0) {
echo 'No Matches Good to add this place';
}else{
echo 'Spot already lived in';
}
?>

I combined the two queries I gave you earlier but it's the same code. Please look at it and study it. It contains the parts you were suppose to substitute.

 

Reverse the 5 and the 3 and run again. This time there is a match in the db and it should catch it. If you are still having problems with this contact me via email this forum.

Link to comment
Share on other sites

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.