Friday 15 June 2012

PHP array data used in javascript graph - codeigniter -


I am using API data in a model that is passed through a controller to see. In a special example, I need a foreach () loop, but I need to get the necessary data in Javascript, please see below how I did it.

I know this is bad behavior, but wanted to know what is the best practice to achieve so that I am going forward.

  Categories: [& lt ;? Php foreach ($ champ_name $ champ_id => $ stat_value) {foreach ($ stat_value as $ cn = & gt; $ cs) {if ($ champ_id! = 0) {echo '' '$ cn.' '', '; }}}? & Gt;]   

Thanks for watching / support.

(PS works on but I know that is not in the right way)

< P> You can use json_encode () to convert an object or array that JavaScript can use.
  Categories: & lt ;? Php echo json_encode ($ myThing); ? & Gt;   

For your use, you will still need your foreach logic so that Jason can be encoded.

If you manually create an array like your query, then you need to focus on the special characters due to breaking the array and syntax errors, for example if your $ cn If any of the double quotes contain, it will generate a syntax error The advantage of JSN encoding is that any special character will be taken care without assuming you.

Example:

  $ arr = array ('abc', 3, 'to' xt ', 6); Echo json_encode ($ arr);   

output

  ["abc", 3, "te \" Xt \ ", 6] ^ qoute is automatically saved  < / Pre>  

No comments:

Post a Comment