0

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 which is "string" which 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 which 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!

1
  • Why are you sending the data to the ajax script in the url? Couldn't you post the data and bypass the step entirely? Commented Jan 29, 2009 at 4:21

2 Answers 2

2

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
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.