Note that there are some explanatory texts on larger screens.

plurals
  1. POCombine several data with their parent?
    text
    copied!<p>Suggestions of how to formulate this question better are welcome.<br> Let's say I have a database query like the following:</p> <pre><code>var dbSize = 5 * 1024 * 1024; // 5MB var db = openDatabase("Oscommerce", "1.0", "Oscommerce Database", dbSize); db.transaction(function(tx) { tx.executeSql('SELECT * FROM customers_basket WHERE customers_id="1"', [], function(tx, results) { var len = results.rows.length, i; var products_options_array = {}; for (i = 0; i &lt; len; i++) { var r = results.rows.item(i); products_options_array[r.customers_basket_id] = r; console.log(products_options_array); } }); }); </code></pre> <p>Below I provide an HTML output example. All attributes inside the <code>div</code>s are the database fields with their corresponding values.</p> <p>The answers I got from jacob and Gaby were based on the HTML below. This was my mistake, because I thought it would be easier to understand my problem if I provided it like that.</p> <pre><code>&lt;div customers_basket_attributes_id="1" customers_id="1" products_id="1{4}2{3}6" products_options_id="4" products_options_value_id="2"&gt;product 1&lt;/div&gt; &lt;div customers_basket_attributes_id="2" customers_id="1" products_id="1{4}2{3}6" products_options_id="3" products_options_value_id="6"&gt;product 1&lt;/div&gt; &lt;div customers_basket_attributes_id="3" customers_id="1" products_id="1{4}3{3}5" products_options_id="4" products_options_value_id="3"&gt;product 1&lt;/div&gt; &lt;div customers_basket_attributes_id="4" customers_id="1" products_id="1{4}3{3}5" products_options_id="3" products_options_value_id="5"&gt;product 1&lt;/div&gt; &lt;div customers_basket_attributes_id="3" customers_id="1" products_id="2{4}3{3}5" products_options_id="4" products_options_value_id="3"&gt;product 2&lt;/div&gt; &lt;div customers_basket_attributes_id="4" customers_id="1" products_id="2{4}3{3}5" products_options_id="3" products_options_value_id="5"&gt;product 2&lt;/div&gt; </code></pre> <p>How can I get it like this:</p> <pre><code>&lt;div products_id="1{4}2{3}6"&gt; &lt;p&gt;product1&lt;/p&gt; &lt;p&gt;products_options_id_4 : products_options_value_id_2&lt;/p&gt; &lt;p&gt;products_options_id_3 : products_options_value_id_6&lt;/p&gt; &lt;/div&gt; &lt;div products_id="1{4}3{3}5"&gt; &lt;p&gt;product1&lt;/p&gt; &lt;p&gt;products_options_id_4 : products_options_value_id_3&lt;/p&gt; &lt;p&gt;products_options_id_3 : products_options_value_id_5&lt;/p&gt; &lt;/div&gt; &lt;div products_id="2{4}3{3}5"&gt; &lt;p&gt;product2&lt;/p&gt; &lt;p&gt;products_options_id_4 : products_options_value_id_3&lt;/p&gt; &lt;p&gt;products_options_id_3 : products_options_value_id_5&lt;/p&gt; &lt;/div&gt; </code></pre> <p>And for more GUI, it should look at the end like <a href="http://jsfiddle.net/whitehat/nvhQx/" rel="nofollow">this <code>JSFiddle</code></a>.</p> <p><strong>SOLVED WITH JACOB'S EXAMPLE:</strong></p> <pre><code>var dbSize = 5 * 1024 * 1024; // 5MB var db = openDatabase("Oscommerce", "1.0", "Oscommerce Database", dbSize); var data = {}; var cart_str = ''; var products_options_array = {}; db.transaction(function(tx) { tx.executeSql('SELECT * FROM customers_basket WHERE customers_id="1"', [], function(tx, results) { var len = results.rows.length,i; for (i = 0; i &lt; len; i++) { var r = results.rows.item(i); products_options_array[r.customers_basket_id] = r; //console.log(products_options_array); } for (key in products_options_array) { var value = products_options_array[key]; customers_basket_id = value['customers_basket_id']; customers_id = value['customers_id']; products_id = value['products_id']; products_options_id = value['products_options_id']; products_options_txt = value['products_options_txt']; products_options_value_id = value['products_options_value_id']; products_options_value_txt = value['products_options_value_txt']; customers_basket_quantity = value['customers_basket_quantity']; final_price = value['final_price']; customers_basket_date_added = value['customers_basket_date_added']; cart_str += '&lt;div customers_basket_attributes_id="' + customers_basket_id + '" customers_id="' + customers_id + '" products_id="' + products_id + '" products_options_id="' + products_options_id + '" products_options_value_id="' + products_options_value_id + '" style="display:none"&gt;' + products_id + '&lt;/div&gt;'; } $('#input').html(cart_str); $('#input div').each(function() { var div = $(this); var productId = div.attr('products_id'); var optionId = div.attr('products_options_id'); if (!(productId in data)) data[productId] = { name: div.text(), options: {} }; if (!(optionId in data[productId].options)) { var optionValueId = div.attr('products_options_value_id'); data[productId].options[optionId] = optionValueId; } }); $.each(data, function(productId, product) { var productDiv = $('&lt;div/&gt;').attr('products_id', productId).appendTo('#output'); $('&lt;p/&gt;').text(product.name).appendTo(productDiv); $.each(product.options, function(optionId, optionValueId) { $('&lt;p/&gt;').text('products_options_id_' + optionId + ' : products_options_value_id_' + optionValueId).appendTo(productDiv); }); }); }); }); </code></pre>
 

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