Jump to content

Store php query array in javascript

Featured Replies

Posted

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

Edited by microsoftdotcom

  • Author

bumping this, since I can get this to work with native php but not in IPB where php syntaxes are slighty changed.

Need help

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"};

 

Archived

This topic is now archived and is closed to further replies.

Recently Browsing 0

  • No registered users viewing this page.