views:

219

answers:

3

Hello. I'm sending some data over AJAX to a PHP file. It's about filter options. Anyway, I am sending it like this:

filter[0][data][type]   string
filter[0][data][value] automobiles
filter[0][field] product

filter[1][data][type] numeric
filter[1][data][value] 6000
filter[1][field] price

This above is taken from FireBug console. Then, in PHP:

$filter = $_POST['filter'.$i];
if (is_array($filter)) {
    for ($i=0;$i<count($filter);$i++){
     switch($filter[$i]['data']['type']){
      case 'string' : 
                        // I'm doing my select from database based on that and so on

So, the translation of this would be: "Get me all the records from database that are: hmm, let's check the filters... I'm getting the first filter type witch is "string" witch needs to be applied to the mysql column named "product"...So I'm searching for the value of "automobiles" there...But I'm not finished yet, the second filter refers to a numeric filter for the column "price" from database. so I'm taking its value and add it to the query. So I'll end up selecting all the automobiles that have a price greater than 6000.

So far so good. The problem is that my way of getting data have changed and I can't send my data in this format no more. The new format is an URL witch looks like this:

filter[0][field]=prodct&filter[0][data][type]=string&filter[0][data][value]=automobiles&filter[1][field]=price&filter[1][data][type]=numeric&filter[1][data][value]=6000

I can do an explode on this one by the "&" and end up with an array...I can do a lot... The problem is that I don't know how to adjust my query building script to work with that kind of received data..So that I can do a "switch($filter[$i]['data']['type']){" again...

Any ideas for modifying the code ?

Thank you!

A: 

I'd send the data over as JSON object, and transform it on php back to an array just like the one you were using before.

On the php you can use json_decode to get the JSON data back to an array, and you can keep using the rest of the code just like before.

Juan
+2  A: 

parse_str($queryString); and then do everything as normal. That method will process the query string and import the variables to the global namespace (which could be dangerous), so maybe use its second form (listed on the manual page):

$result = array();
parse_str($queryString, $result)
$filters = $result['filter'];

foreach($filters as $filter) {
// your code
}
Ray Hidayat
A: 

Thank you for your help. It works

Manny Calavera
Artelius