0

am using drupal_http_build_query and have part of my code as follows:

$or = db_or();
$or->condition('cid',$fField1);
$or->condition('cid',$fField2);
$or->condition('cid',$fField3);
$or->condition('cid',$fField4);
$or->condition('cid',$fField5);
$or->condition('cid',$fField6);
$or->condition('cid',$fField7);
$or->condition('cid',$fField8);

$querydata  = db_select('webform_submitted_data', 'wsd')
  ->fields('wsd',array('data'))
  ->condition('nid', $nodeID, '=')
  ->condition('sid', $sID, '=')         
  ->condition($or)
  ->execute()
  ->fetchAll();


$result = array();
$i = 1;
foreach ($querydata as $qvalue) {
    $key = 'mapField'.$i;
    $result[] = array($$key => $qvalue->data);
    $i++;
}

$post_data = drupal_http_build_query($result,'', '&');

When printing the $post_data variable value, am getting something like this: name=john&1[address]=street1&2[phone]=123456789

Notice the 1[] and 2[] after the &in the printed value. Any fix to remove these so that I get name=john&address=street1&phone=123456789?

1
  • As side note, drupal_http_build_query() only uses two parameters, not three; since you are using the default value for the second one, you can simply use drupal_http_build_query($result).
    – avpaderno
    Commented Jul 10, 2015 at 15:09

1 Answer 1

0

You are getting that result because you build $result as an array whose values are array. Since drupal_http_build_query() contains the following code, the result you obtain is by design.

  foreach ($query as $key => $value) {
    $key = ($parent ? $parent . '[' . rawurlencode($key) . ']' : rawurlencode($key));

    // Recurse into children.
    if (is_array($value)) {
      $params [] = drupal_http_build_query($value, $key);
    }
    // If a query parameter value is NULL, only append its key.
    elseif (!isset($value)) {
      $params [] = $key;
    }
    else {
      // For better readability of paths in query strings, we decode slashes.
      $params [] = $key . '=' . str_replace('%2F', '/', rawurlencode($value));
    }
  }

You should change the code to the following one.

foreach ($querydata as $qvalue) {
  $key = 'mapField' . $i;
    $result[$$key] = $qvalue->data;
    $i++;
}
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.