Invision Community 4: SEO, prepare for v5 and dormant account notifications By Matt Monday at 02:04 PM
microsoftdotcom Posted January 2, 2016 Posted January 2, 2016 I cant store my php array in javascript. Been trying to fix the problem for a few days, but It keeps saying that my json_encode is undefined. I hope someone can help me with this. This is my array in php: {{$counter;}} {{$memberArray = array();}} {{$query = \IPS\Db::i()->select( 'name', 'core_members', 'member_group_id = 6 or member_group_id = 4 or member_group_id = 3');}} {{foreach $query as $name}} {{$counter++;}} {{$memberArray[$name] = $counter;}} {{endforeach}} var_dumping the array gives me this: array(4) { ["Robert"]=> int(1) ["Peter"]=> int(2) ["William"]=> int(3) ["Edward"]=> int(4) } This is the code I use to store php variable in javascript: var ar = json_encode($memberArray); But when I loop the array in javascript it says my json_encode is undefined. I dont know what I'm doing wrong Ideally I would like my php array to be stored in a way so that I can call it like this in Javascript: ar['Peter'] returns me 1
microsoftdotcom Posted January 3, 2016 Author Posted January 3, 2016 bumping this, since I can get this to work with native php but not in IPB where php syntaxes are slighty changed. Need help
Marcher Technologies Posted January 4, 2016 Posted January 4, 2016 Because the template engine isn't actually parsing that php, the js thinks it is a javascript function to call, hence the error. This should work: var ar = {expression="json_encode($memberArray)" raw="1"};
Recommended Posts
Archived
This topic is now archived and is closed to further replies.