views:

167

answers:

1

I am using Wordpress with JQuery to create a vehicle selection form.

Vehicle information is retrieved by JQuery Ajax calls to a PHP backend script. The PHP scripts query an SQLite database for vehicle information.

Wordpress form -> JQuery get -> PHP script -> SQLite DB

I have three distinct queries that need to be executed:

  1. Vehicle makes available
  2. Vehicle models for a specific vehicle make
  3. Vehicle years for a specific vehicle make and model

I could create a custom PHP script that accepts a "query_type" parameter from the JQuery get call. The PHP script would then run the appropriate SQL query. That option sounds mediocre at best.

Should I create a custom PHP file for each of the three queries?

OR

Is there a neat PHP tool that will create something similar to .NET's Service Endpoints?

The Service Interface Pattern

Thanks Everyone!

+1  A: 

Use JQuery to post the "type" of query you want to get. You should only need one PHP script, just let the PHP determine what type of data is being requested.

You might want to look into returning JSON data as it's easier to deal with using JQuery: JQuery Docs - getJSON

As far as getting PHP to understand what's being requested, use a query string in your URL: request.php?r=make, or request.php?m=datsun&r=model, or request.php?ma=datsun&mo=280zx&r=year

Then in PHP:

switch($_GET['r']){
  case "make":
    //return request for makes here
  case "model":
    //return request for model here, checking for existence of make request and validity
  case "years":
    //return request for vehicle years, based on existence of other two variables.
}
snicker
Thanks for the answer. It is the "just let the PHP determine what type of data is being requested" part that I could use a nice tool kit for. JSON does seem to be the best data transfer format for the job.
dbasch
I suppose using a JSON Data Transfer Object and a PHP DTO handler function, would be pretty reusable for service endpoint queries. A standard framework is always nice though.
dbasch
For "letting PHP determine the data requested" I added a little bit of code for a simple PHP script, if you want to handle that yourself. Hope that helps!
snicker
Thanks. I ended up passing JSON between the JQuery Javascript and the PHP script. I used a PHP query type switch similar to the one you suggested. If I ever find a PHP tool that provides a Service Interace pattern, I will update this question.
dbasch
Good luck! Glad to help.
snicker