2011-12-30 56 views
0

如何更好地制定这个问题的建议是值得欢迎的。
比方说,我有一个数据库查询类似如下:将几个数据与他们的父母结合?

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 < len; i++) { 

      var r = results.rows.item(i); 
      products_options_array[r.customers_basket_id] = r; 
      console.log(products_options_array); 
     } 

    }); 
}); 

下面我提供一个HTML输出的例子。 div中的所有属性都是具有相应值的数据库字段。

我从雅各和盖比那里得到的答案是基于下面的HTML。这是我的错误,因为我认为如果我以这种方式提供它,会更容易理解我的问题。

<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">product 1</div> 
<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">product 1</div> 

<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">product 1</div> 
<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">product 1</div> 

<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">product 2</div> 
<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">product 2</div> 

我怎样才能得到它像这样:

<div products_id="1{4}2{3}6"> 
<p>product1</p> 
<p>products_options_id_4 : products_options_value_id_2</p> 
<p>products_options_id_3 : products_options_value_id_6</p> 
</div> 

<div products_id="1{4}3{3}5"> 
<p>product1</p> 
<p>products_options_id_4 : products_options_value_id_3</p> 
<p>products_options_id_3 : products_options_value_id_5</p> 
</div> 

<div products_id="2{4}3{3}5"> 
<p>product2</p> 
<p>products_options_id_4 : products_options_value_id_3</p> 
<p>products_options_id_3 : products_options_value_id_5</p> 
</div> 

而对于更多的GUI,它应该看看像this JSFiddle结束。

解决与雅各的例子:

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 < 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 += '<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">' + products_id + '</div>'; 

     } 

     $('#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 = $('<div/>').attr('products_id', productId).appendTo('#output'); 
      $('<p/>').text(product.name).appendTo(productDiv); 

      $.each(product.options, function(optionId, optionValueId) { 
       $('<p/>').text('products_options_id_' + optionId + ' : products_options_value_id_' + optionValueId).appendTo(productDiv); 
      }); 
     }); 

    }); 
}); 
+1

哪儿了'customers_basket_attributes_id'和'customers_id'去?你想放弃它们吗? – 2011-12-30 00:48:40

+0

在他们不需要的输出中,我想我提供了我必须产生输出的数据。 – wHiTeHaT 2011-12-30 00:50:41

+0

对不起,如果这是一个愚蠢的问题,但你的样本中只有“产品1”吗?如果您应该有不同的产品,那么适当地命名它们以更好地理解您的问题会很有帮助。 – Craig 2011-12-30 01:01:31

回答

1

我会打破这种分为三个步骤:

  1. 迭代通过源文件。
  2. 构建一个有代表性的数据结构。
  3. 构建输出文档。

步骤#产生的数据结构2应该是这个样子:

{ 
    '1{4}2{3}6': { 
     name: 'product1', 
     options: { 
      '4': '2', 
      '3': '6' 
     } 
    }, 
    '1{4}3{3}5': { 
     name: 'product1', 
     options: { 
      '4': '3', 
      '3': '5' 
     } 
    }, 
    '2{4}3{3}5': { 
     name: 'product1', 
     options: { 
      '4': '3', 
      '3': '5' 
     } 
    } 
} 

要构建结构,尝试这样使用jQuery:

var data = {}; 
$('#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 = $('<div/>').attr('products_id', productId).appendTo('#output'); 
    $('<p/>').text(product.name).appendTo(productDiv); 

    $.each(product.options, function(optionId, optionValueId) { 
     $('<p/>') 
      .text(
       'products_options_id_' + optionId 
       + ' : products_options_value_id_' + optionValueId) 
      .appendTo(productDiv); 
    }); 
}); 

显示结果的。

+0

你的小提琴似乎非常适合使用从webDB检索的输出。 – wHiTeHaT 2011-12-30 01:57:40

+0

我希望你在不久的将来能找到一份好工作满足你的愿望你的工作应该包含什么 – wHiTeHaT 2011-12-30 02:16:45

+0

谢谢。你也是。 – Jacob 2011-12-30 17:20:54

1

这适用于此示例。我希望这是非常明显的,所以你可以使其适应实际数据..

var uniqueIdList = {}; // this will hold the unique product_id's 
// find all divs with products_id attribute and store that id 
var products = $('div[products_id]').each(function(){ 
    var id = $(this).attr('products_id'); 
    if (uniqueIdList[id] === undefined){ 
     uniqueIdList[id] = true; 
    } 
}); 
for (var uId in uniqueIdList){ // for each product_id 
    var div = $('<div>', {id: uId}).appendTo('body'); // create a new div (container) and assign the id 
    products.filter('[products_id="'+uId+'"]').each(function(idx){ // find all divs that have the same product_id 
     var self = $(this); 
     var option = self.attr('products_options_id'), 
      value = self.attr('products_options_value_id'); // extract the info 

     if (idx === 0) { // for the first of each group extract the text (assuming it is the same for all of same group) 
      $('<p>',{text: self.text()}).appendTo(div); // ad text to div (as a p tag) 
     } 
     $('<p>',{text: option + ' : ' + value}).appendTo(div); // add info to the div 
    }) 
} 

演示在http://jsfiddle.net/gaby/dRFCh/1/

+0

omg,我只是把自己搞砸了,因为我是愚蠢的给出一个输出的例子作为一个html输出,但是我需要找到一种方法来使用你的例子从我的dB输出。我真的很抱歉这个问题你做了一个greath工作。 – wHiTeHaT 2011-12-30 01:35:11

+0

@ wHiTeHaT,是的..我在读到你对这个问题的评论后明白了,但已经太晚了:)不用担心......那么你使用什么后端语言?和什么DB平台? – 2011-12-30 01:39:40

+0

我使用webDB,作为sqlite,大多数mysql语句工作。所以我输出的数据是数据库变量。我在我的“爱好项目”的最后一个阶段,从购物车中读取数据。 – wHiTeHaT 2011-12-30 01:45:00

相关问题