1

I have a php-function to fetch events as objects and I want to add them to my existing ul-list via JQuery. I want to transfer the php-variable to JQuery and want to iterate this variable to build up the HTML. I tried it with this one:

$pastEvents = event::getPastEvents($team);
var test = { pastEvents : '<?php echo $pastEvents; ?>' };
for (obj in test.pastEvents) {          
    console.log(obj);
}

The result i get is only 0, 1, 2, 3, 4, 5,... in the console. The var_dump from PHP looks like this:

array(8) { [0]=> object(game)#10 (17) { ["creatorid":protected]=> int(1) ["tid":protected]=> int(1) ["eid":protected]=> int(12) ["art":protected]=> int(1) ["spielart":protected]=> int(1) ["zeitpunkt":protected]=> string(19) "2012-02-28 11:20:00" ["zeit":protected]=> string(19) "2012-02-18 11:21:50" 

How can i access the objects from PHP? How can i iterate the variable correctly, that i can access the data like obj.creatorid ... ?

Thanks!

P.S: I could solve this by using jquery's ajax-function - but before the user clicks i already have the data available - so the ajax-request is needless.

3 Answers 3

1

You need to use php method json_encode() to convert to json.

echo json_encode( $array);

Then in jQuery ( using ajax)

$.getJSON( url, function(data){
   console.log( data)// log the whole json object to console
})
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. Yes, i want to debug this step by step and my first result after "echo json_encode($pastEvents);" is "[{},{},{},{},{},{},{},{}]" -> how can that be?
very strange since var_dump showed properly. Need php5 but it would have failed if json_encode wasn't a function. Don't understand all the empty objects ..sorry
I now tried it with this: for (var i = 0, len = test.length; i < len; ++i) { var even = test[i]; alert(even.creatorid); } len is 8, that's correct. But alerts have just the result "undefined". Puh, that's frustrating :(
GOT IT!!! It was hidden somewhere in my brain! The variables in the classes must be public and not protected. Now it works :) thanks!
alert will not display an object...use browser console to log to, will save you a lot of development time learning to use it and it's not hard
0

You need to use JSON. Look up the JSON and the PHP functions json_encode and json_decode.

Comments

0

Use JSON possibilites. Convert your php data to JSON. That can be easily understood from Javascript.

json_encode($myData);
json_decode($myData);

To read, JSON, in Javascript, simply access it as an object.

myObject.myField

1 Comment

Thanks for your quick response. I tried it like this: echo json_encode($pastEvents); but the result is "[{},{},{},{},{},{},{},{}]" -> but the var_dump of $pastEvents is the result from above. How can that be...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.