coderhut Posted July 5, 2013 Share Posted July 5, 2013 Hello experts. My question might be simple but I am stuck on it since yesterday. I have little complex thing to do but the basic issue for me at the moment is this //A controller function function getOdds(){ $numbers = array(1,2,3,4,5,6,7,9); //pass these numbers to jquery for returning odd numbers. How can I pass these numbers to jquery and get back results in this same controller function? $oddNumbers = ..do what here to get jquery result..? } Quote Link to comment https://forums.phpfreaks.com/topic/279884-cakephp-get-results-from-jquery-within-controller-function/ Share on other sites More sharing options...
Psycho Posted July 5, 2013 Share Posted July 5, 2013 Why are you wanting to have a PHP function pass variables to JavaScript to do something? Why not just make the PHP function return the odd numbers. What you are asking sounds backwards to me. It is common to pass data from JavaScript to PHP to do something since PHP operates on the back-end and has access to things such as the database. It doesn't make sense to me to pass data to JavaScript to be processed. Quote Link to comment https://forums.phpfreaks.com/topic/279884-cakephp-get-results-from-jquery-within-controller-function/#findComment-1439504 Share on other sites More sharing options...
coderhut Posted July 5, 2013 Author Share Posted July 5, 2013 (edited) I don't want to get odd numbers from javascript. I just tried to explain my problem in a very simple way. My focus isn't on getting odd numbers but communicating with jquery from within controller's function. Edited July 5, 2013 by coderhut Quote Link to comment https://forums.phpfreaks.com/topic/279884-cakephp-get-results-from-jquery-within-controller-function/#findComment-1439505 Share on other sites More sharing options...
coderhut Posted July 6, 2013 Author Share Posted July 6, 2013 I don't want to get odd numbers from javascript. I just tried to explain my problem in a very simple way. My focus isn't on getting odd numbers but communicating with jquery from within controller's function. Quote Link to comment https://forums.phpfreaks.com/topic/279884-cakephp-get-results-from-jquery-within-controller-function/#findComment-1439606 Share on other sites More sharing options...
coderhut Posted July 6, 2013 Author Share Posted July 6, 2013 I would like to actually put my real problem as odd numbers confused others. Here is what I want to achieve within controller function. //a controller function function getPlaces(){ $allRideOfferPlaces = $this->rideOffer->find('all'); foreach($allRideOfferPlaces as $place){ //how can I call google api for returning lat/long information of $place } } Quote Link to comment https://forums.phpfreaks.com/topic/279884-cakephp-get-results-from-jquery-within-controller-function/#findComment-1439612 Share on other sites More sharing options...
Solution jcbones Posted July 6, 2013 Solution Share Posted July 6, 2013 (edited) All Google API request are HTTP request, and returned as JSON or XML. I am assuming that $allRideOfferPlaces is an array that contains reference codes from previously pulled places? I also noticed that there is not an option to send in batches of references. Reference: Google Place API Detailed *note* There is a missing ? and a missing index in the code below. So you need to go through it before you use it. function getPlaces() { $allRideOfferPlaces = $this->rideOffer->find('all'); //set return of method to variable. $requestParams[] = 'key=myKey'; //make sure you put your key here: $requestParams[] = 'sensor=false'; //This device doesn't have GPS. foreach($allRideOfferPlaces as $place) { //foreach array value (should be references). $requestParams['reference'] = 'reference=' . $place; //assign the reference key (changes on each request). $googleApiUri = 'https://maps.googleapis.com/maps/api/place/details/json' . implode('&',$requestParams); //assign the params to the request URI $json = file_get_contents($googleApiUri); //send the request; $decoded = json($json,true); //decode the json into an array: $results[] = $decoded; //add the results to an array; //lat and long of this place: echo $decoded['name'] . ' is located at Lat: ' . $decoded['results']['geometry']['lat'] . ' and Long: ' . $decoded['results']['geometry']['lng'] . '<br />'; } echo '<pre>' . print_r($results,true) . '</pre>'; } Edited July 6, 2013 by jcbones Quote Link to comment https://forums.phpfreaks.com/topic/279884-cakephp-get-results-from-jquery-within-controller-function/#findComment-1439642 Share on other sites More sharing options...
coderhut Posted July 9, 2013 Author Share Posted July 9, 2013 Thank you jcbones for such a detailed solution. Though I ended up setting the php $allRideOfferPlaces for view file and did all google map related task in view and later created the js array and sent back that js array using ajax to controller function which fetched its view for my required results. Quote Link to comment https://forums.phpfreaks.com/topic/279884-cakephp-get-results-from-jquery-within-controller-function/#findComment-1440009 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.