0

I am trying to get the Devbridge Autocomplete jQuery script to work, and I am oh so very close. I can get it to give me suggestions (dropdown values) however I need to use it's data attribute.

The suggested JSON formatting as as follows:

{
    suggestions: [
        { value: "United Arab Emirates", data: "AE" },
        { value: "United Kingdom",       data: "UK" },
        { value: "United States",        data: "US" }
]
}

So far, I have managed this:

{
"suggestions": [
    "Show Name 1",
    "Show Name 2"
],
"data": [
    "1",
    "2"
]
}

The code producing that output is as follows:

$reply = array();
$reply['suggestions'] = array();
$reply['data'] = array();

while ($row = $result->fetch_array(MYSQLI_ASSOC))//loop through the retrieved values
{
    //Add this row to the reply
    $reply['suggestions'][]=$row['SHOW_NAME'];
    $reply['data'][]=$row['SHOW_ID'];
}

//format the array into json data
echo json_encode($reply);

Any suggestions? I can't figure out how to combine the two data elements into one array, let alone prepend them with 'value' or 'data'...

Coffeee
  • 143
  • 1
  • 2
  • 13

3 Answers3

1
$response = array();
$reply = array();
while ($row = $result->fetch_array(MYSQLI_NUM))//loop through the retrieved values
{
    //Add this row to the reply
    $reply['value'] = $row[0];
    $reply['data'] = $row[1];
    $response['suggestions'][] = $reply;
}
//format the array into json data
echo json_encode($response, JSON_PRETTY_PRINT);
DevlshOne
  • 8,357
  • 1
  • 29
  • 37
1
while($row = $result->fetch_array(MYSQLI_ASSOC))
{

$rec = array();

$rec['value'] = $row['SHOW_NAME'];
$rec['data'] = $row['SHOW_ID'];

$payload['suggestions'][] = $rec;

}

echo json_encode($payload);
Bob Arezina
  • 462
  • 5
  • 16
  • `$payload` is never initialized and will result in a PHP Warning. Also, the use of `JSON_PRETTY_PRINT` is recommended when returning the variable back to a Javascript function for improved debugging. – DevlshOne Jul 23 '13 at 13:24
0

Not sure if i have got you correctly but if you mean to get both values in one array then use :

$replay[][array('country' => $row['SHOW_NAME'],'data' => $row['SHOW_ID'])];
munzx
  • 375
  • 5
  • 13