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'...

1

3 Answers 3

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

1 Comment

$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.
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);

Comments

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'])];

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.