Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 18    Views: 78

PaddyO
Lost but trying
Profile
Posts: 189
Reg: Sep 11, 2013
Geelong
5,190
03/04/14 11:16 PM (10 years ago)

Can I use a variable in MySQL calculations?

Hi all, I'm working my way towards a locator search based on the user's current location. The three snippets below show the evolution of my understanding of how this works in an external dataURL. Except that the last one doesn't work. If anyone has any ideas that would be awesome. 1. This works: $SQL = "SELECT * FROM location WHERE postal_code='78756'"; 2. This works (gets all records in the table that are within 100miles of the specified hard-coded coordinates): $SQL = "SELECT *, ( 3959 * acos( cos( radians(41.881944) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(-87.627778) ) + sin( radians(41.881944) ) * sin( radians( latitude ) ) ) ) AS distance FROM location HAVING distance < 100"; 3. But this doesn't (tries to get all records within 100miles of the users current location): $userlat = $_GET['latitude']; $userlong = $_GET['longitude']; $SQL = "SELECT *, ( 3959 * acos( cos( radians('$userlat') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('$userlong') ) + sin( radians('$userlat') ) * sin( radians( latitude ) ) ) ) AS distance FROM location HAVING distance < 100"; I've played with removing the ' from '$userlat', replacing with ", to no avail. Any thoughts on what else I should try? Cheers Paddy
 
Susan Metoxen
buzztouch Evangelist
Profile
Posts: 1706
Reg: May 01, 2011
Hopkins, Minnes...
26,260
like
03/04/14 11:33 PM (10 years ago)
So is it getting the users latitude and longitude? Can you test that with a log statement? I think you would get the users lat and long via the dataURL.
 
PaddyO
Lost but trying
Profile
Posts: 189
Reg: Sep 11, 2013
Geelong
5,190
like
03/04/14 11:47 PM (10 years ago)
Hi Susan, I'm not sure how I'd go about getting a log statement.. would I be able to get one via the simulator? Maybe I could modify a custom htmlDoc to just echo those results (for the purpose of testing)? I guess I'm relying on the simulator with its 'pretend' location to return its coordinates correctly... maybe that's unrealistic? The users lat and long would always come from their phone so I need the app to be able to get those, and they'll be different for each user and for each location that users move to. Unless I'm missing something (likely!!) I'm not sure how a dataURL could supply those coordinates - or have I misunderstood? Thanks Paddy
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
03/05/14 12:39 AM (10 years ago)
It might be easier just to take 'all' records from the database and sort them once your query is returned. Although it doesn't 'draw the line' at a certain distance, my script in menu01.php of the 'loadScreenObject' kit does sort by distance. https://dl.dropboxusercontent.com/u/115208762/loadScreenObject.zip To modify it (well, in your script) all you'd need to do is test for desired distance (if $distance > 100) 'before' any record gets added to the array, and those that are 'within' get in the array, and those that are 'too far' do not…
 
PaddyO
Lost but trying
Profile
Posts: 189
Reg: Sep 11, 2013
Geelong
5,190
like
03/05/14 12:52 AM (10 years ago)
Thanks Smug - I think it's the simulator, perhaps not the code, as example #2 above sorts by distance beautifully. My challenge is to somehow dig into the phone itself and get the lat/long of the user's current position. That's where I thought $userlat = $_GET['latitude']; $userlong = $_GET['longitude']; would work, as I see variations of that in a few BT folks' code, so I figured that it was defined somewhere in the map plugin already. To test it, I installed your location SMS/email plugin and I see that it shows the lat/long of the location at the bottom of the screen - the green numbers, right? Seems that no matter where I simulate a location for the iOS simulator, those coordinates stay as 0,0. So the pretend location setting doesn't seem to go through to your plugin, nor to an echo for those variables that I added to another screen. I'll keep scratching! Cheers Paddy.
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
03/05/14 02:14 AM (10 years ago)
Well, don't forget the built in 'merge fields' in *any* BT device… When the device send 'any' URL, you can 'attach' these and it will provide the data you need. But *you* attach them. They're not always there. The documentation is for BTv1.5, but the same rules apply in this instance… http://www.buzztouch.com/docs/v1.5/mergeFields.php Cheers! -- Smug And for the record, I am *seriously* impressed with example #2. That really looks like a Syntax nightmare. I wouldn't have tried it, lol! I'm smiling at your success! Nice going!
 
Susan Metoxen
buzztouch Evangelist
Profile
Posts: 1706
Reg: May 01, 2011
Hopkins, Minnes...
26,260
like
03/05/14 07:11 AM (10 years ago)
@PaddyO, we need you on the Buzztouch Developer list. Is it ok if I add you?
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
03/05/14 07:23 AM (10 years ago)
Formula looks fine, so I'm with Sue - you need to check whether your variables have valid data. Start by having XCode output the URL to debugger. That will give you a good starting point. In your script, you can echo $SQL rather than whatever else you're outputting to the screen, then load the URL from XCode into a desktop browser for easy testing. One other thing: you should be careful using statements like $userlat = $_GET['latitude']; If there is no $_GET['latitude'] variable set, the script will crash with an error. I usually start my scripts with this function: Function getPost($var) { $ret = ""; If (isset($_POST[$var])) $ret = $_POST[$var]; else if (isset($_GET[$var])) $ret = $_GET[$var]; else if (isset$_REQUEST[$var)) $ret = $_REQUEST[$var]; return $var; } Now, you can set your variables without worry: $userlat = getPost("latitude"); Also makes it REST compatible. ------------------------------------------------------- https://www.buzz-tools.com
 
Niraj
buzztouch Evangelist
Profile
Posts: 2943
Reg: Jul 11, 2012
Cerritos
37,930
like
03/05/14 07:53 AM (10 years ago)
That mergeFields is the cat's meow. Susan -- you found another fantastic developer! :-)
 
Susan Metoxen
buzztouch Evangelist
Profile
Posts: 1706
Reg: May 01, 2011
Hopkins, Minnes...
26,260
like
03/05/14 01:34 PM (10 years ago)
We may touch on this post at the meeting at the top of the hour.
 
PaddyO
Lost but trying
Profile
Posts: 189
Reg: Sep 11, 2013
Geelong
5,190
like
03/05/14 03:37 PM (10 years ago)
Thanks everyone, so my discovery for the day is mergeFields. That is exactly what I was looking for, just didn't know it existed!! Thanks again Smug! Chris, thanks for the note of caution and your code up there. I had a niggle at the back of my mind that what I had was just too simple to be true. Will give it a shot. Susan & Niraj, that scares me!! Honestly I'm just cobbling together bits of whatever I can find 'out there' and trying to learn as I go... I'm nowhere near the level of understanding of you guys; I'm just fortunate to be able to steal whatever falls off your plates. If you have a category for 'muddling wannabes', that's probably more like me. :) Cheers Paddy
 
PaddyO
Lost but trying
Profile
Posts: 189
Reg: Sep 11, 2013
Geelong
5,190
like
03/05/14 03:55 PM (10 years ago)
.. btw that snippet #2 above I found in the middle of this https://developers.google.com/maps/articles/phpsqlsearch_v3?hl=en which is a Google Developer how-to. I just ignored all the XML stuff and stripped it back a bit. (In my case, 'latitude' and 'longitude' aren't magical operators, they're the column names in my dbase table.) Others might find that link useful too. Cheers Paddy
 
PaddyO
Lost but trying
Profile
Posts: 189
Reg: Sep 11, 2013
Geelong
5,190
like
03/05/14 04:48 PM (10 years ago)
Hi Chris, I'm struggling to understand what you've suggested: "Start by having XCode output the URL to debugger. That will give you a good starting point. In your script, you can echo $SQL rather than whatever else you're outputting to the screen, then load the URL from XCode into a desktop browser for easy testing." Echo SQL - do you mean just output the raw query results instead of formatting them as JSON? How do I put that into Xcode or get a URL out of it? Sorry, this is a totally new thing for me - if you could explain a little more I would be really grateful. Thanks Paddy
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
03/05/14 05:39 PM (10 years ago)
Actually I meant echo the $SQL variable itself just to see what the actual statement being passed to the MySQL database actually is.
 
PaddyO
Lost but trying
Profile
Posts: 189
Reg: Sep 11, 2013
Geelong
5,190
like
03/05/14 06:27 PM (10 years ago)
OK, I think I found what's happening. It's basically that the iOS simulator won't reveal its location to the php when asked, because any time the php tries to get it, it just spits the dummy and gives a 0,0 coordinate, then decides that 0,0 is unfindable, like this: 2014-03-06 14:36:28.922 noshplanet[32809:70b] BT_strings: mergeBTVariablesInString (before): http://www.noshplanet.com/pins.php?deviceLatitude=[deviceLatitude]&deviceLongitude=[deviceLongitude] 2014-03-06 14:36:28.922 noshplanet[32809:70b] BT_strings: mergeBTVariablesInString (after merge): http://www.noshplanet.com/pins.php?deviceLatitude=0&deviceLongitude=0 2014-03-06 14:36:28.922 noshplanet[32809:70b] BT_downloader: INIT 2014-03-06 14:36:28.923 noshplanet[32809:70b] BT_downloader: downloadFile: http://www.noshplanet.com/pins.php?deviceLatitude=0&deviceLongitude=0 2014-03-06 14:36:28.923 noshplanet[32809:70b] BT_screen_map: downloadFileStarted: starting download... 2014-03-06 14:36:29.821 noshplanet[32809:70b] BT_screen_map: downloadFileCompleted: ERROR-1968: URL returned 400 - location not available This is even when I have set the simulator to be in a pretend location and I can see the pulsing dot on the simulator screen. I guess that's the difference between 'simulator' and 'real phone', eh? :/ So I guess my question now is, is there any other way to test this where I can use a merge field and see a result?
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
03/06/14 01:46 PM (10 years ago)
Hmm… it 'should' work, simulator or not… For the moment, if you need a work around, just 'append' the URL with the desired coordinates… "dataURL":"http://www.noshplanet.com/pins.php?deviceLatitude=13.5&deviceLongitude=144.8" Cheers! -- Smug
 
PaddyO
Lost but trying
Profile
Posts: 189
Reg: Sep 11, 2013
Geelong
5,190
like
03/06/14 04:21 PM (10 years ago)
Thanks Smug, great idea! That 'works', at least it doesn't trip up Xcode and I'm not getting the error messages any more, thank you. But I still need to work on getting those values passed through to the MYSQL query, which isn't happening yet. Onward!!
 
Susan Metoxen
buzztouch Evangelist
Profile
Posts: 1706
Reg: May 01, 2011
Hopkins, Minnes...
26,260
like
03/06/14 05:14 PM (10 years ago)
I have the video from yesterday posted now. It may help.
 
PaddyO
Lost but trying
Profile
Posts: 189
Reg: Sep 11, 2013
Geelong
5,190
like
03/06/14 07:08 PM (10 years ago)
And now... I am thrilled to close this thread - it works. Thank you everyone - including David for using my challenge as a developer video topic. Wow. Such a great community. If anyone is interested to see the shiny bits of code that got me where I needed to for this little feature, I'm more than happy to share. I hope I can help someone out there one day too. Now, onto the next feature/bug/learning_experience. :) Cheers Paddy.
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.