Jump to content

cakephp - get results from jquery within controller function


Go to solution Solved by jcbones,

Recommended Posts

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..?

}

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.

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 by coderhut

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


	}

}
  • Solution

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 by jcbones

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.

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.