I put this in another thread but was threaded in with a few other issues. I'm down to one remaining 2-part problem. I see a number of examples on how to use json_encode, but they always stop before going into the json_decode side of things. I have an array that used to go through 4 Java class files and stored to a Google datastore, I have to bypass Java and run it through json instead. Issue is I must keep the output syntax as it is to avoid having to completely re-write the navigational javascript, and I just need a nudge in how to make the syntax work in a multi-layered array. The output is pulled by jQuery and loaded as so:
PHP Code:
function load_articles()
{
global $pages, $activePages, $ofy, $localeclass, $articleclass, $pageclass, $langcode, $contents;
The $ofy was originally assigned to call com.googlecode.objectify class file, now it needs to point to ('example.mysql.com', 'user', 'pswd') The commented out json_decode line shows this is how it worked before the project moved to Java, but the locale information I have completely striped as I am not doing multiple language support. The array itself is where I need help on the correct syntax to assign the values in such a way they can be called with jQuery as shown above ^.
PHP Code:
$articleclass = array(
array(
'stub' => 'welcome',
'numberOfPages' => 1,
'title' => 'Welcome to blah blah blah!',
'templates' => array(
'template-start-7'
),
'active' => '1',
'hidden' => '1',
'order' => 0
),
array(
'stub' => 'about',
'numberOfPages' => 2,
'title' => 'More about yadda yadda yadda',
'subtitle' => 'More seductive than the Dark Side.',
'active' => '1',
'templates' => array(
'template-start-7',
'template-inner-5'
),
'order' => 1
)
);
I've tried various ways but I always end up with Fatal error: Call to a member function query() on a non-object on $Articles = $ofy->query($articleclass)->order('order')->list(); and yes, I have tried to reassign $ofy to point to the database I attempted to send the json_encode data to. I originally tried to serialize the data before realizing I can't run a query on it if it's serialized.
Please help, at this point I'm desperate and am prepared to offer a ritual sacrifice of my favorite coffee cup to the gods of the internets!
Why not simply use JSON.parse() and JSON.stringify() methods that are built into JavaScript (and which all modern browsers support). If you really need it for antiquated browsers as well (such as IE7) then https://github.com/douglascrockford/...aster/json2.js provides the equivalent functionality for those antiques.
Why not simply use JSON.parse() and JSON.stringify() methods that are built into JavaScript (and which all modern browsers support). If you really need it for antiquated browsers as well (such as IE7) then https://github.com/douglascrockford/...aster/json2.js provides the equivalent functionality for those antiques.
I'll look into that, thanks. My main thing is trying to do as little re-writing as possible, because the php code for loading and parsing the array data is good, it's just the packaging method for the array that's changed.
And I won't need help with old browsers, the project is in HTML5 and CSS3, if an old browser accesses it they'll be reverted back to a stripped down (ugly) version with a message on top linking them to where they can update their browsers.
I'll look into that, thanks. My main thing is trying to do as little re-writing as possible, because the php code for loading and parsing the array data is good, it's just the packaging method for the array that's changed.
And I won't need help with old browsers, the project is in HTML5 and CSS3, if an old browser accesses it they'll be reverted back to a stripped down (ugly) version with a message on top linking them to where they can update their browsers.
Since you are using HTML 5 and CSS 3 there is no reason why you shouldn't also expect the JSON object to exist in the browser as i was introduced into browsers much earlier than HTML 5 support.
Since you are using HTML 5 and CSS 3 there is no reason why you shouldn't also expect the JSON object to exist in the browser as i was introduced into browsers much earlier than HTML 5 support.
Oh I don't expect otherwise, it's just while my skills are pretty decent with HTML and CSS, when it comes to PHP I'm just okay. When it comes to javascript, to me, it's like reading French: I can pick out a word here and there, and I can get the gist of what the code is doing by reading it through, just don't ask me to write or speak it!
When it comes to javascript, to me, it's like reading French:
Perhaps the examples on the site in my sig will help you with writing JavaScript then. Decide what you want to do and then look at the particular example that deals with how to do that particular thing. JavaScript is similar enough to PHP that you shouldn't have too much trouble working out how to convert anything you'd do in PHP to the approximate JavaScript equivalent.
Perhaps the examples on the site in my sig will help you with writing JavaScript then. Decide what you want to do and then look at the particular example that deals with how to do that particular thing. JavaScript is similar enough to PHP that you shouldn't have too much trouble working out how to convert anything you'd do in PHP to the approximate JavaScript equivalent.
Thanks, checking it out now. I've decided to try json_encode from the above issue, assigned $ofy as follows:
It passed the line that gave the error in the original post, gave an issue with another line (my fault, I removed code for an option to print the book, since that will not be part of this project, removed an if statement, forgot to correct the elseif below it) Once I fixed that, I'm now getting a Parse error: syntax error, unexpected T_ARRAY in /html/locale/chapter/configuration.php on line 53, which is the line just below $ofy = json_encode(array("chapters" => $chapters).
This is my biggest hurdle, the proper syntax for the array, it's driving me crazy. So I'll check out your site and see what I can find there, all the array examples I've seen through google searches have all been simple, this one is sadly not because each individual 'page' of a 'chapter' has its own template style to call on the in css. And from one example to the next I've found the syntax has never been exactly the same twice, so I can't make heads or tails of it!
Last edited by Mishka; 01-17-2012 at 08:17 AM..
Reason: typo
A new wrinkle, or rather the original wrinkle rediscovered...
I originally thought json_encode wasn't working as I needed it to which is why I was looking at alternatives. I did a var_dump and it shows the array reformatted perfectly (though I still can't figure out how to name the sub-array in a multidimensional array, this is very important for the rest of the code! The parent array is called 'chapters', the sub array needs to be named 'pages', still need help on how to format that). Still getting Fatal error: Call to a member function getProperty() on a non-object, the object it is trying to call is 'chapters' from the array. Encodes with $json = json_encode($ofy);, and the line that fails is: $Articles = json_decode( $json->getProperty('chapters')->getValue() );
I logged into my SQL database directly and found my PHP file is accessing the database and creating the table as it should, but the table was empty! When I serialize the data, it will load to the database, but only the first 30 characters (even when I set the character limit to 500). So I copied the encoded array from my var_dump and saved it as a .csv file and tried uploading it directly to the table to see what happened, it displays all but the last array item and gives an error: Unknown column '' in 'field list' SQL command. Since it's reporting an error it just scraps the information and will not save it to the table.
I know this moves us away from a javascript issue and into an SQL issue, but does anyone have any thoughts on this? And please, please, please how do I create a sub-array name for the 'templates' items from the original post?
Fixed the database, remaining issue is still parsing the array
Fixed the database issue by changing the table from varchar to TEXT and adding the line mysql_query("SET TEXTSIZE 2147483647");. Now I can move the encoded array to and from SQL, but I still have Fatal error: Call to a member function query() on a non-object on line: $Articles = json_decode( $json->query('chapters')->order('order')->list() );. I try parsing the encoded info with $decoded = json_decode($json [$assoc = true]; then var_dump($decoded); and get Warning: json_decode() expects parameter 1 to be string, object given. If I just use json_decode without trying to convert it back to an array, I get my array in string form:
Code:
array(7) {
[0]=> object(stdClass)#2 (7) {
["stub"]=> string(7) "welcome"
["numberOfPages"]=> int(1)
["title"]=> string(28) "Welcome to the site!"
["templates"]=> array(1) {
[0]=> string(16) "template-start-7" }
["active"]=> string(1) "1"
["hidden"]=> string(1) "1"
["order"]=> int(0) }
[1]=> object(stdClass)#3 (7) {
["stub"]=> etc. and so forth...
arranged in a more pleasing eye-candy layout by yours truly for your viewing pleasure. :)
The first array should be assigned an object value "book", the object(stdClass)#2-8 should have a value of "chapters", and the child array ["templates"]=>array should have a value of "pages". This is apparently why the faulty code is returning a non-object, because it can't find "chapters", where that would be found in the decoded string instead shows object(stdClass). This is how it is converting the original format:
So now I can get it to encode, upload to SQL, return from SQL when called, and decoded. I just need to get the object names assigned properly. This, I think, has been my only real problem from the beginning, but because of the SQL issue I wasn't able to properly see it.
Can anyone help me figure out how to assign these 3 array levels as objects with the names I specified; either during the json_encode process, or when decoding the data for parsing? I tried to find real-world examples of using json.stringify, but I found nothing that made sense to my javascript ignorant brain.