I am returning results from a MySql database in PHP that I would like to use the json_encode function to turn into a string to pass back for another page's usage to display options within a SELECT element. However, the json_encode method fails and my page is not rendering properly. By that I mean I receive a generic IIS error from the cgi-bin process. I have error capturing setup within my script but the error generated is not boiling up far enough to be shown and I am not sure why.
The basis of my question is how can I make json_encode like the information below or do I need to roll my own encoder to deal with it?
Array
(
[0] => stdClass Object
(
[productID] => 2
[optionID] => 1
)
[1] => stdClass Object
(
[productID] => 2
[optionID] => 2
)
[2] => stdClass Object
(
[productID] => 2
[optionID] => 3
)
[3] => stdClass Object
(
[productID] => 2
[optionID] => 4
)
)
Spell json_encode correctly and it works MUCH better... dummy me.
Related
I am trying to extract data from a php object that is actually retrived from the google blogger post feed. I tried using javascript and it works fine with dot notations. But I am not being able to do same with php.
Here is how the objects look like
stdClass Object
(
[id] => stdClass Object
(
[$t] => tag:blogger.com,1999:blog-8275521076679012362.post-3869147994923309099
)
[published] => stdClass Object
(
[$t] => 2017-12-20T07:02:00.000-08:00
)
[updated] => stdClass Object
(
[$t] => 2018-01-12T07:38:12.068-08:00
)
[category] => Array
(
[0] => stdClass Object
(
[scheme] => http://www.blogger.com/atom/ns#
[term] => stacks
)
)
[title] => stdClass Object
(
[type] => text
[$t] => New #85 & 86: 2day Stack and LastPublished Stack
)
)
There are many but I am showing only few here.
Here is my code that I have tried.
for ($i=0; $i <$len ; $i++) {
$thisPost = $feedData->feed->entry[$i];
print_r($thisPost); // The above object is printed because of this line.
}
The problem is, I am not being able to get the title or anything from the object.
You're probably having trouble because of the $t property names.
If you try to access them with, for example, $thisPost->title->$t, it won't work because PHP will interpret the $t as a variable, which probably doesn't exist.
You can use complex syntax to specify that it's just a string.
$thisPost->title->{'$t'}
First, I'm using sugarcrm pro 6.5 and accessing via rest v4, so I have this array that's being returned from printing $results that is working fine:
stdClass Object
(
[result_count] => 2000
[total_count] => 3390
[next_offset] => 2000
[entry_list] => Array
(
[0] => stdClass Object
(
[id] => 77da301b-83dd-4fe6-e38f-53ba151fb084
[module_name] => Leads
[name_value_list] => stdClass Object
(
[id] => stdClass Object
(
[name] => id
[value] => 77da301b-83dd-4fe6-e38f-53ba151fb084
)
[name] => stdClass Object
(
[name] => name
[value] => Jim Beam
)
[status] => stdClass Object
(
[name] => status
[value] => Dead
)
[website] => stdClass Object
(
[name] => website
[value] => website.com
)
[phone_cr] => stdClass Object
(
[name] => phone_cr
[value] => 1-888-888-8888
)
)
)
[1] => stdClass Object
(
[id] => d0ecc069-d556-98f3-41f2-53ba1468327a
[module_name] => Leads
[name_value_list] => stdClass Object
(
[id] => stdClass Object
(
[name] => id
[value] => d0ecc069-d556-98f3-41f2-53ba1468327a
)
[name] => stdClass Object
(
[name] => name
[value] => John Doe
)
[status] => stdClass Object
(
[name] => status
[value] => New
)
[website] => stdClass Object
(
[name] => website
[value] => web.com
)
[phone_cr] => stdClass Object
(
[name] => phone_cr
[value] => 1-888-888-8888
)
)
)
I'm using a query from the api to filter the results for the user I'm targeting:
'query' => "leads.assigned_user_id='user_ID-here'",
'order_by' => "date_entered DESC",
This works fine. So I've ran a foreach () statement to retrieve only one field on a button click, which also works just fine. What I really need to accomplish is before this statement, a foreach() command (or something else?) to filter out and retrieve ONLY the "New" results in the status value, and from that group output an array showing only the website field. Seen in the "desired end result section of this question."
This is the code I'm filtering the field I'm targeting and having a new array created with if that helps bridge the gap:
$results = call('get_entry_list', $params, $url);
$eresult = array();
foreach ($results->entry_list as $index=>$value_list) {
$listed = $value_list->name_value_list->website->value;
$eresult[] = $listed;}
So the desired end result based on this data should be:
Array
(
[1] => web.com
)
I'm unsure what I need to do to filter the "Status" field to only then be ran with the $eresult array I created to achieve this. To be clear, everything is working as it should, and my print from $eresult is outputting exactly as it should by returning all results in the website value area, I just need some help to get it sorted before going to that step by sorting it by the "new" status first without all the extra 'stuff,' then sorting out the array in my desired format with the foreach() statement above. I tried to cut out all the other code, as it's a pretty long project, so this should be all the relevant information for the particular goal I need to accomplish in this segment. Any help is greatly appreciated! Thank you!
I've decided to create a second script for this as a temp solution by adding:
'query' => "(leads.assigned_user_id='user_ID-here') AND (status='New')"
So I guess that works, I was trying to avoid calling another script for just one separate function, but it is working fine.
When I have an array like this :
Array (
[0] => Array ( [0] => 1 [1] => 12 [2] => Essener [3] => 1 )
[1] => Array ( [0] => 2 [1] => 12 [2] => Dinkel Spezial [3] => 0.2 )
[2] => Array ( [0] => 1 [1] => 1 [2] => Essener [3] => 1 )
)
and I use json_encode and echo it, I get this:
[["1","12","Essener","1"],["2","12","Dinkel Spezial","0.2"],["1","1","Essener","1"]]
which is good for me.
Now I have an array with stdClass Objects, which I wasn't able to transform into JSON with json_encode. When I echo it, it just doesn't show anything.
Then I transformed this array with objects to an array like this (with get_object_vars()):
Array (
[0] => Array (
[item_id] => 1
[item_name] => Essener
)
[1] => Array (
[item_id] => 2
[item_name] => Dinkel Spezial
)
[2] => Array (
[item_id] => 3
[item_name] => Saatenbrot
)
)
and when I use json_encode and echo it still doesn't show anything. Can anyone tell me what I am doing wrong or what I need to do to get a JSON array?
I need this json array to send data to an IOS App.
From the documentation:
Note:
In the event of a failure to encode, json_last_error() can be used to determine the exact nature of the error.
So you can try to detect the exact error by yourself. From your information I can't see any error.
Furthermore I don't think, it doesn't return anything. Try to var_dump() the result of json_encode(). I assume it returns false, which means an error occured.
So if anybody wondered what was wrong,
the problem was that i had "ä,ü,ö,ß" in my array and i needed to convert these to unicode and then everything worked just fine.
I have using cakephp for my website. And i use sql server 2012 with it.
I have confused that when i use:
$this->set('types',$this->Manager->query('select * from product_types'));
to get the array of all my product types
the return array is:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[name] => hoa my pham
)
)
[1] => Array
(
[0] => Array
(
[id] => 2
[name] => hoa my
)
)
)
Why has the [0] instead of [product_types]????
Please follow the documentation and tutorials available.
Then you will be able to just use
$this->set('managers', $this->Manager->find('all'));
for the very same thing - using a clean approach with wrapper functions and a sql server datasource.
For SqlServer there should be a datasource available, for example:
https://github.com/cakephp/datasources/blob/2.0/Model/Datasource/Database/Sqlsrv.php
This is from the CakePHP documentation:
To use the model name as the array key, and get a result consistent with that returned by the Find methods, the query can be rewritten:
$this->Picture->query("SELECT * FROM pictures AS Picture LIMIT 2;");
which returns:
Array
(
[0] => Array
(
[Picture] => Array
(
[id] => 1304
[user_id] => 759
)
)
[1] => Array
(
[Picture] => Array
(
[id] => 1305
[user_id] => 759
)
)
)
So you need:
$this->set('types',$this->Manager->query('select * from product_types as Product_Types'));
Source: http://book.cakephp.org/2.0/en/models/retrieving-your-data.html
P.S.:
This syntax and the corresponding array structure is valid for MySQL
only. Cake does not provide any data abstraction when running queries
manually, so exact results will vary between databases.
Take the following JSON string (generated by some ExtJS code - but that's irrelevant):
[{"action":"Setting","method":"toggle","data":["welcome-home"],"type":"rpc","tid":2},{"action":"ContentExtFeFillout","method":"todo","data":[true,0,8,false],"type":"rpc","tid":3}]
being sent to a server as a POST request and retrieved via $GLOBALS['HTTP_RAW_POST_DATA'].
Running
json_decode($GLOBALS['HTTP_RAW_POST_DATA']);
on our development machine (5.2.10-2ubuntu6.4 with Suhosin Patch 0.9.7) gives a correct print_r() output of:
Array
(
[0] => stdClass Object
(
[action] => Setting
[method] => toggle
[data] => Array
(
[0] => welcome-home
)
[type] => rpc
[tid] => 2
)
[1] => stdClass Object
(
[action] => ContentExtFeFillout
[method] => todo
[data] => Array
(
[0] => 1
[1] => 0
[2] => 8
[3] =>
)
[type] => rpc
[tid] => 3
)
)
Running the same code on a client's production machine (5.2.5 with Suhosin Patch 0.9.6.2 and Zend Optimizer; SUSE Linux by the way) gives the following print_r() output:
Array
(
[0] => stdClass Object
(
[action] => Setting
[method] => toggle
[data] => Array
(
[0] => welcome-home
)
[type] => rpc
)
[1] => 2
[2] => stdClass Object
(
[action] => ContentExtFeFillout
[method] => todo
[data] => Array
(
[0] => 1
[1] => 0
[2] => 8
[3] =>
)
[type] => rpc
)
[3] => 3
)
Note the missing tid property which obviously has been moved into the main array as an own value - this naturally breaks all the following code.
We also downloaded a Windows PHP version 5.2.5 to check if there's a bug in json_decode() but we get the correct output here.
Are there any known issues with json_decode() at all that could cause this odd behavior?
We're currently totally clueless...
Thanks to all of you!
Best regards
Stefan
OK guys - problem solved. Lacking any more options we persuaded the client to update the installed PHP version and guess what: it works.
There seems to have been a subtle bug in their PHP installation (PHP, Zend Optimizer and/or Suhosin) which has been fixed with the update. Still, a quite weird thing.
Thanks to all of you!
Best regards
Stefan
Have you tried swapping the tid and type keys? Also how about using a hardcoded variable to check if the problem could be with $GLOBALS['HTTP_RAW_POST_DATA']?
Try the following:
$t1='[{"action":"Setting","method":"toggle","data":["welcome-home"],"type":"rpc","tid":2},{"action":"ContentExtFeFillout","method":"todo","data":[true,0,8,false],"tid":2,"type":"rpc"}]';
print_r(json_decode($t1));