title: Count categories from parent 0
body: <p>I'm trying to do a count of categories and output the result for each <code>parent_id = 0</code> in <em>JavaScript</em> or <em>jQuery</em> (if it has the appropriate functions).
  I have:</p>
  
  <pre><code> categories_id - parent_id
   10            -      3
   19            -      2
   17            -      1
   13            -      3
   12            -      3
   15            -      3
   4             -      1
   8             -      1
   16            -      1
   9             -      1
   6             -      1
   5             -      1
   11            -      3
   18            -      2
   7             -      1
   20            -      2
   14            -      3
   22            -      4
   1             -      0
   2             -      0
   3             -      0
   21            -      0
  </code></pre>
  
  <p>Data comes dynamically from a database.
  Then if I visit a <code>categories_id</code> from my list, I require to add a counter that says <code>category X</code> has <code>XX categories</code>.
  This is the script in which I require it:</p>
  
  <pre><code>var dbSize = 5 * 1024 * 1024; // 5MB
  var db = openDatabase("Oscommerce", "1.0", "Oscommerce Database", dbSize);
  
  var categories={};
  
  var list_str = '';
  db.transaction(function (tx) {
  tx.executeSql('SELECT c.categories_id, c.categories_image, c.parent_id, cd.categories_name, COUNT(p2c.categories_id) AS total FROM categories c INNER JOIN categories_description cd ON c.categories_id = cd.categories_id LEFT JOIN products_to_categories p2c ON p2c.categories_id = c.categories_id GROUP BY c.categories_id, cd.categories_name ORDER BY c.sort_order, cd.categories_name', [], function (tx, results) {
  list_str += '&lt;ul data-role="listview" data-inset="true" data-theme="d" data-count-theme="a" data-split-theme="a"&gt;';
  var len = results.rows.length, i;
  
  for (i = 0; i &lt; len; i++) {
  var r = results.rows.item(i);
  categories[r.categories_id] = r;
  //console.log(categories);
  console.log(r.categories_id+'-'+r.parent_id);
  }
  
  for(key in categories)
  {
  
  var parent = 0;  
  var value=categories[key];
  catId = value['categories_id'];
  catName = value['categories_name'];
  catImage = value['categories_image'];
  parentId = value['parent_id'];
  total = value['total'];
  
  if (parentId == parent) 
  {  
  
  if (total == 0){    
  list_str += '&lt;li id="'+ catId +'"&gt;&lt;a class="parentlink" parentid="'+ parentId +'" catid="'+ catId +'" catname="'+ catName +'"&gt;&lt;h3&gt;' + catName + '&lt;/h3&gt;&lt;p&gt;' + catImage + '&lt;/p&gt;&lt;div class="ui-li-count"&gt;'+ total +' products&lt;/div&gt;&lt;/a&gt;&lt;a class="parentlink" parentid="'+ parentId +'" catid="'+ catId +'" catname="'+ catName +'"&gt;somelink&lt;/a&gt;&lt;/li&gt;';
  }else{
  list_str += '&lt;li id="'+ catId +'"&gt;&lt;a class="parentlink" parentid="'+ parentId +'" catid="'+ catId +'" catname="'+ catName +'"&gt;&lt;h3&gt;' + catName + '&lt;/h3&gt;&lt;p&gt;' + catImage + '&lt;/p&gt;&lt;div class="ui-li-count"&gt;'+ total +' products&lt;/div&gt;&lt;/a&gt;&lt;a href="#"&gt;somelink&lt;/a&gt;&lt;/li&gt;';
  }
  
  }
  }
  
  list_str += '&lt;/ul&gt;';
  
  $('#parents').html(list_str).find('ul').listview();
  
  
  }); 
  });
  </code></pre>