I have a php file that pulls the values from a field in my database for the json to pass it to a script I'm building for an autocomplete field.
It works fine, however I want to be able to use multiple values in the field separated by a comma delimiter. What I have now pulls the entire value as one value and I can't figure out how to break it down into separate values to be displayed in the autocomplete suggestions.
I've tried to explode the $row[cb_activities] and it does place the " " around the commas but then my script gives me an undefined value.
As is when you load the php file you get the following:
Code:
([{"cb_activitiesterm":"Kicking Cats, Working"},{"cb_activitiesterm":"Web Development"}])
If I explode the value it looks like this:
Code:
([{"cb_activitiesterm":"Kicking Cats","Working"},{"cb_activitiesterm":"Web Development"}])
But I guess what I really need it to look like is this:
Code:
([{"cb_activitiesterm":"Kicking Cats}, {"cb_activitiesterm":"Working"},{"cb_activitiesterm":"Web Development"}])
Any ideas on how to accomplish this?
Here's my current php file:
Code:
<?php
//connection information
$host = "localhost";
$user = "myuser";
$password = "mypassword";
$database = "mydatabase";
$param = $_GET["term"];
//make connection
$server = mysql_connect($host, $user, $password);
$connection = mysql_select_db($database, $server);
//query the database
$query = mysql_query("SELECT cb_activities FROM jos_comprofiler WHERE cb_activities REGEXP '^$param'");
//build array of results
for ($x = 0, $numrows = mysql_num_rows($query); $x < $numrows; $x++) {
$row = mysql_fetch_assoc($query);
$activities[$x] = array(cb_activitiesterm => $row[cb_activities]);
}
//echo JSON to page
$response = $_GET["callback"] . "(" . json_encode($activities) . ")";
echo $response;
mysql_close($server);
?>