View Full Version : How to split field value into separate values w/comma delimiter

11-30-2010, 09:40 PM
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:

([{"cb_activitiesterm":"Kicking Cats, Working"},{"cb_activitiesterm":"Web Development"}])

If I explode the value it looks like this:

([{"cb_activitiesterm":"Kicking Cats","Working"},{"cb_activitiesterm":"Web Development"}])

But I guess what I really need it to look like is this:

([{"cb_activitiesterm":"Kicking Cats}, {"cb_activitiesterm":"Working"},{"cb_activitiesterm":"Web Development"}])

Any ideas on how to accomplish this?

Here's my current php file:


//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;



11-30-2010, 10:29 PM
Here's a little bit of thinking outside the box ...


$string="([{\"cb_activitiesterm\":\"Kicking Cats, Working\"},{\"cb_activitiesterm\":\"Web Development\"}])";


echo $three;

// now you can explode it ... by commas


11-30-2010, 11:04 PM
I tried playing around with this method a bit but can't get it to work.
I end up just getting Array([array]).
The string has to be called from the database, not just typed out in the php file, so I can't add the slashes. Trying to break it down like you were aiming for just didn't work by doing this.

12-01-2010, 01:00 AM
If you remove the parenthesis at the beginning and end, it becomes a valid JSON string. Then just run it through json_decode().

12-01-2010, 02:28 AM
I had to put the slashes in there to make the quotes part of the variable.

$items is an array.

$items[0] = Kicking Cats
$items[1] = Working
$items[2] = Web Development

We have no way to test it with your database ... so you're on your own there.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum