Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-30-2010, 09:40 PM   PM User | #1
sfraise
Regular Coder

 
Join Date: Mar 2009
Posts: 175
Thanks: 3
Thanked 1 Time in 1 Post
sfraise is an unknown quantity at this point
How to split field value into separate values w/comma delimiter

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);
	
?>

Last edited by sfraise; 11-30-2010 at 11:06 PM..
sfraise is offline   Reply With Quote
Old 11-30-2010, 10:29 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,056
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Here's a little bit of thinking outside the box ...

PHP Code:
<?php

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

$one=str_replace("([{\"cb_activitiesterm\":\"","",$string);
$two=str_replace("\"},{\"cb_activitiesterm\":\"",",",$one);
$three=str_replace("\"}])","",$two);

echo 
$three;

// now you can explode it ... by commas
$items=explode(",",$three);

?>
mlseim is offline   Reply With Quote
Old 11-30-2010, 11:04 PM   PM User | #3
sfraise
Regular Coder

 
Join Date: Mar 2009
Posts: 175
Thanks: 3
Thanked 1 Time in 1 Post
sfraise is an unknown quantity at this point
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.

Last edited by sfraise; 12-01-2010 at 12:28 AM..
sfraise is offline   Reply With Quote
Old 12-01-2010, 01:00 AM   PM User | #4
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,503
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
If you remove the parenthesis at the beginning and end, it becomes a valid JSON string. Then just run it through json_decode().
__________________
ZCE
kbluhm is offline   Reply With Quote
Old 12-01-2010, 02:28 AM   PM User | #5
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,056
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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.
mlseim is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:53 AM.


Advertisement
Log in to turn off these ads.