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 8 of 8
  1. #1
    New Coder
    Join Date
    Mar 2010
    Posts
    40
    Thanks
    7
    Thanked 0 Times in 0 Posts

    json decode not creating an array?

    I'm going mad with this one!

    I am using json to decode posted data and then looping through a foreach statement to UPDATE a mysql database. I have customised the code provided here: http://webdeveloperplus.com/jquery/s...g-drop-panels/

    Thing is everything worked fine on my local test server (Mac OSX) but uploading to live server the script does not work. I was getting an 'Invalid argument supplied for foreach()' statement which I've found shows if there is no array to loop through.

    I use Firebug to see what json is creating and it seems to be doing it's job and outputs the same string as on my local server.

    I'm guessing to sort this out you'll need more info - let me know what and I'll happily oblige. (PS: magic_quotes_gpc is on in both cases)

    Thanks to anyone taking this on (it's probably something really basic!)

  • #2
    Regular Coder
    Join Date
    Feb 2009
    Location
    NJ, USA
    Posts
    476
    Thanks
    2
    Thanked 70 Times in 69 Posts
    Hey, first, you should probably post this in the php forum.

    But, have you tried echo'ing back the json string that your php page is receiving? Magic quotes will in fact mess up json_decode(), and cause json_decode() to return null.

    Try echo'ing the json string that your php page receives and view it as the response in firebug (comment out all of your other php code for now). If you see a bunch of backslashes from magic quotes, then you'll need to run the json through the stripslashes() function to remove them.

    Not sure why they would be working on your local server with magic quotes turned on as well though, as they definitely mess up json_decode(). Are you sure that they are definitely turned on for your local server?
    Code:
    if( get_magic_quotes_gpc() ) {
        $jsonString = stripslashes( $jsonString );
    }
    $data = json_decode( $jsonString );
    If that's not the issue tho, let me know, and post up your php code if you could. Btw, is the same version of php running on both servers?

    -Greg

  • #3
    New Coder
    Join Date
    Mar 2010
    Posts
    40
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Yes wasn't sure if it was a PHP or JSON related issue so thought I would start here. Below is the code I'm using to try and update the database:

    Code:
    if (get_magic_quotes_gpc()) {
    function stripslashes_deep($value) {
    $value = is_array($value) ? array_map('stripslashes_deep', $value) :
    stripslashes($value);
    return $value;}
    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
    $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
    }
    
    $data=json_decode($_POST['data']);
    
    if (is_array($data)) {
    
    	foreach($data->items as $item)
    	{
    			
    		//Extract column number for panel
    		$col_id=preg_replace('/[^\d\s]/', '', $item->column);
    		//Extract id of the panel
    		$widget_id=preg_replace('/[^\d\s]/', '', $item->id);
    		
    		$item->order = (int)$item->order;
    		$item->collapsed = (int)$item->collapsed;
    
    		$sql="UPDATE exp_widgets SET column_id='" .$col_id. "', sort_no='".$item->order."', collapsed='".$item->collapsed."' WHERE id='".$widget_id."'";
    		
    		//$sql="UPDATE exp_widgets SET column_id='7' WHERE id='" .$widget_id. "'";
    		mysql_query($sql) or die('Error updating widget DB');
    			
    	}
    	
    echo "success";
    	
    } else {
    
    echo "For some unknown reason there is no array!!";
    
    }
    and here is the output from the JSON decode:

    Code:
    data	{"items":[{"id":"item1","collapsed":0,"order":0,"column":"column1"},{"id":"item4","collapsed":0,"order":1,"column":"column1"},{"id":"item9","collapsed":0,"order":0,"column":"column2"},{"id":"item8","collapsed":0,"order":1,"column":"column2"},{"id":"item11","collapsed":0,"order":2,"column":"column2"},{"id":"item10","collapsed":0,"order":3,"column":"column2"},{"id":"item13","collapsed":0,"order":4,"column":"column2"},{"id":"item12","collapsed":0,"order":5,"column":"column2"},{"id":"item2","collapsed":0,"order":0,"column":"column3"},{"id":"item3","collapsed":0,"order":1,"column":"column3"},{"id":"item15","collapsed":0,"order":2,"column":"column3"},{"id":"item17","collapsed":0,"order":3,"column":"column3"},{"id":"item5","collapsed":0,"order":4,"column":"column3"},{"id":"item16","collapsed":0,"order":5,"column":"column3"},{"id":"item7","collapsed":0,"order":6,"column":"column3"},{"id":"item6","collapsed":1,"order":7,"column":"column3"},{"id":"item14","collapsed":0,"order":8,"column":"column3"}]}
    PHP version on live server is 5.2.13 and on local test server 5.2.11

    Any ideas?

  • #4
    Regular Coder
    Join Date
    Feb 2009
    Location
    NJ, USA
    Posts
    476
    Thanks
    2
    Thanked 70 Times in 69 Posts
    Ohh, ok, now it makes sense

    The problem is that your outermost "element" of the json string is { and }, meaning "object". So json_decode() is converting that to an object, not an array. You can see this by doing:
    Code:
    print_r( $data );
    In fact, your code would work perfectly just without that outer if statement, lol.

    Or, the correct thing to test for in that if statement would be:
    Code:
    if( is_array( $data->items ) )

  • #5
    New Coder
    Join Date
    Mar 2010
    Posts
    40
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Sorry but that's not working either. Al the extra lines in the code I posted before I had added to check if the json decode function was creating an array or not. I have simplified the code now but am getting my original error of 'Invalid argument supplied for foreach()...' which I'm guessing is because the foreach loop isn't receiving an array. However, the decoded values seem to be in the correct format.

    I've simplified both the code and the amount of items so you can see the decoded values easier.

    Decoded values:
    Code:
    {"items":[{"id":"item1","collapsed":0,"order":0,"column":"column1"},{"id":"item4","collapsed":0,"order":1,"column":"column1"},{"id":"item6","collapsed":1,"order":7,"column":"column3"}]}
    Simplified code:
    Code:
    $data=json_decode($_POST['data']);
    
    	foreach($data->items as $item)
    	{
    			
    		$col_id=preg_replace('/[^\d\s]/', '', $item->column);
    
    		$widget_id=preg_replace('/[^\d\s]/', '', $item->id);
    
    		$sql="UPDATE exp_widgets SET column_id='" .$col_id. "', sort_no='".$item->order."', collapsed='".$item->collapsed."' WHERE id='".$widget_id."'";
    
    		mysql_query($sql) or die('Error updating widget DB');
    			
    	}
    	
    echo "success";
    Is the decoded string in the correct format? It's the only thing I can see that would stop the foreach accepting the values.

    Any further ideas?...

  • #6
    New Coder
    Join Date
    Mar 2010
    Posts
    40
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Actually maybe I should have read your response properly! Are you saying I just remove the outer { and } from the decoded response?

    so it looks like:
    Code:
    "items":[{"id":"item1","collapsed":0,"order":0,"column":"column1"},{"id":"item4","collapsed":0,"order":1,"column":"column1"},{"id":"item6","collapsed":1,"order":7,"column":"column3"}]
    I'll give it a go.

  • #7
    New Coder
    Join Date
    Mar 2010
    Posts
    40
    Thanks
    7
    Thanked 0 Times in 0 Posts
    OK have now fixed it. I was getting confused with what Firebug was telling me and what was actually going on. I should have echoed out the posted variables before they got to the json decode function and that would have given me the clue.

    There was backslashes in the variables so json couldn't decode. Simple fix by adding:

    Code:
    $data=json_decode(stripslashes($_POST['data']));
    doh!

    Many thanks GjSlick for helping me to figure it out. (Still don't know how it worked on the local server but I haevn't got the time to try and work that out!)

  • #8
    Regular Coder
    Join Date
    Feb 2009
    Location
    NJ, USA
    Posts
    476
    Thanks
    2
    Thanked 70 Times in 69 Posts
    Well I'm glad that you got it working, but you didn't take my advice in my first post!
    Quote Originally Posted by Gjslick View Post
    Try echo'ing the json string that your php page receives and view it as the response in firebug (comment out all of your other php code for now). If you see a bunch of backslashes from magic quotes, then you'll need to run the json through the stripslashes() function to remove them.
    The json that you would have been seeing in firebug would have just been the json being sent to the page, before magic quotes on the php side would have done its thing. However, I'm surprised that all of that code to remove slashes didn't work...

    ---------

    Quote Originally Posted by jockturner View Post
    Are you saying I just remove the outer { and } from the decoded response?
    Nooo, you need the outer { and }. Part of the json format. But when running it in json_decode(), $data would have been an object, and you were testing if it was an array with the is_array() function. If you had used is_object() instead, then that if statement would have worked as you had planned


    But anyway, yeah, really weird that it worked on your local server but not your production server. You def sure that you have magic quotes turned on for your local? Who knows sometimes with php.. lol. Glad that you got it working and all though.

    All the best,

    Greg


  •  

    Tags for this Thread

    Posting Permissions

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