Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Mar 2009
    Posts
    175
    Thanks
    3
    Thanked 1 Time in 1 Post

    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.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,472
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    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);

    ?>

  • #3
    Regular Coder
    Join Date
    Mar 2009
    Posts
    175
    Thanks
    3
    Thanked 1 Time in 1 Post
    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.

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    If you remove the parenthesis at the beginning and end, it becomes a valid JSON string. Then just run it through json_decode().

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,472
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •