Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use JSON. see why JSON over XML: <a href="http://thephpcode.blogspot.com/2008/08/why-json-over-xml-in-ajax.html" rel="noreferrer">http://thephpcode.blogspot.com/2008/08/why-json-over-xml-in-ajax.html</a></p> <p>To use json in PHP simply:</p> <pre><code>&lt;?php echo '&lt;script type="text/javascript"&gt;/* &lt;![CDATA[ */'; echo 'var train = '.json_encode($array); echo '/* ]]&gt; */&lt;/script&gt;'; ?&gt; </code></pre> <p>the variable <code>train</code> in Javascript will be an object containing properties and arrays based on your PHP variable <code>$array</code>.</p> <p>To parse or iterate through the train object, you can use <code>for ... in</code> statements for JSON arrays, and directly use object.property for JSON objects.</p> <p>See this:</p> <pre><code>&lt;?php $array = array(array('id'=&gt;3,'title'=&gt;'Great2'),array('id'=&gt;5,'title'=&gt;'Great'),array('id'=&gt;1,'title'=&gt;'Great3')) echo '&lt;script type="text/javascript"&gt;/* &lt;![CDATA[ */'; echo 'var train = '.json_encode($array); echo '/* ]]&gt; */&lt;/script&gt;'; ?&gt; </code></pre> <p>The output will be:</p> <pre><code> var train = [{id:3,title:'Great2'},{id:5,title:'Great'},{id:1,title:'Great3'}]; </code></pre> <p>the variable <code>train</code> becomes an array of objects. [] squrare brackets are arrays, holding more arrays or objects. {} curly braces are objects, they have properties to them.</p> <p>So to iterate the <code>train</code> variable:</p> <pre><code>&lt;script type="text/javascript"&gt; var train = [{id:3,title:'Great2'},{id:5,title:'Great'},{id:1,title:'Great3'}]; for(i in train){ var t = train[i]; // get the current element of the Array alert(t.id); // gets the ID alert(t.title); // gets the title } &lt;/script&gt; </code></pre> <p>Simple! Hope that really helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload